• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

TECHDIIP

  • Tech
  • Windows
  • Powershell
  • Scripts
  • News
  • Games

PowerShell Advanced Functions

PowerShell Advanced Functions, the standard functions do not have built-in parameters, and functionality is limited, to make a function advanced we need to use the keyword CmdletBinding() under the function declaration, yes that’s it. We use the advanced function to avoid the mundane of writing the code again and again.

The advanced functions have many built-in functionalities that you don’t get them with simple functions, let’s see the difference with examples.

#Simple function

Function Get-Devicesize {

param($ComputerName)

$drive = Get-WmiObject Win32_logicaldisk -Filter ‘Deviceid = “c:”‘

$properties = @{

ComputerName = $ComputerName
FreeSpace = $drive.FreeSpace
DeviceID = $drive.DeviceID
Size = $drive.Size

}

New-Object -TypeName PSobject -Property $properties

$properties | Export-Csv -Path C:\temp\deviceinfo.csv -Force -NoTypeInformation

}

if you run the above simple function you get the below results.

PS C:\> Get-Devicesize -ComputerName DESKTOP-SV9FFDGD

FreeSpace           Size                 ComputerName            DeviceID
——— —- ———— ——————————————-
50582523904 106820530176   DESKTOP-SV9FFDGD          C:

It doesn’t give more options or functions when I try to explore with operation -, see the screenshot.

Let’s use the same script and make it as advanced function, remember as I mentioned above we need to use the parameter CmdletBinding() to make the function advanced.

The same script, however adding CmdletBinding() under the function.

Function Get-Devicesize{

[ComdletBinding()]

param($ComputerName)

$drive = Get-WmiObject Win32_logicaldisk -Filter 'Deviceid = "c:"'

$properties = @{
            
     ComputerName = $ComputerName
     FreeSpace = $drive.FreeSpace
     DeviceID = $drive.DeviceID
     Size = $drive.Size

     }

New-Object -TypeName PSobject -Property $properties

}

When you run the above-advanced function you get the same output, however, you get other functions that allow you to explore more options, see how it works.

when you make function it will open up all the world of options, we use these in our script to perform additional tasks.

Adding some useful Advanced function terminology.

Item Usage
Cmdlets The commands available in the powershell we call cmdlets, we have to import modules to get with respective tasks or application automation.
Confirm This gives a pop-up asking us to confirm while executing the code or script
Debug This command gives us debug information, this is helpful for troubleshooting the script.
ErrorAction This is the default variable available in the powershell and most useful when you dealing with errors.
ErrorVariable This is a default variable and holds errors information of powershell code
Functions Powershell functions are the most lightweight form of powershell command, it’s a grouping of code runs against each object defined (within that code) in the script block and performs the specified action. Powershell functions help the administrator to create reusable code which acts as powershell cmdlets.
OutBuffer This command gives the object count and uses when you are dealing with the pipeline.
OutVariable This command gives us the information of a variable which holds output information
Scripts The powershell scripts extension ends .ps1 and we can’t directly open it, we have to edit and run in powershell interface itself.
Verbose This command gives us detailed information of a task while executing the script.
What if This is a command (cmdlet) that gives information about what happens if you execute code or command.

Conclusion:

I would strongly recommend test these examples scripts output and see how they are working, I have not explained them in detail, just mentioned the logic of the functions and how they work, as we progressing in learning the code will understand more about the functions and advanced function, I just covered the basic things and will add more in upcoming topics, thank you.

Thank you for reading this article, if you have any questions please let us know.

Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • How do Zoom Application Testing | Working or Not
  • Powershell script to test website status up or down on a local or remote computers
  • Solved: Server execution failed windows 10 Server 2016 and 2019
  • How to Fix windows 10 version 1903 error 0xc1900223
  • Free Music Making Software List One Can Try Out
  • About Us
  • CONTACT US
  • Disclaimer
  • Privacy Policy

Copyright © 2023 techdiip.com