Saturday, July 24, 2021

How to create and use custom PowerShell Functions

How to create and use custom PowerShell Functions

PowerShell variable $Profile runs automatically when a shell session is started and it can be used to run scripts and set variables.

For a function to work on your session:

1) Identify the "*profile.ps1"  location by executing $profile. 

2) Open the "*profile.ps1" file in a Notepad with administrator privilege.

3) Copy & paste the function script.

4) Save the file.

For a function to work on everyone's session:

1) Navigate to "C:\Windows\System32\WindowsPowerShell\v1.0".

2) Create and save a PowerShell script (eg: FunctionScript.ps1) exclusively for the function in *.ps1 format.

3) Create a new or open exsisting "*profile.ps1" file in a Notepad with administrator privilege.

4) Type the following to import the function and save the file.

Import-Module C:\Windows\system32\WindowsPowerShell\v1.0\FunctionScript.ps1

No comments:

Post a Comment