Powershell Script Base
Posted on March 23, 2021 (Last modified on July 11, 2024) • 1 min read • 133 wordsEasy powershell function template:
# VERY USEFUL:
# https://adamtheautomator.com/powershell-functions/
function Personal-Function {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string] $UserPrincipalID
)
Write-Host "Hey ho "$UserPrincipalID
}
Save as myfunction.ps1
, execute like this:
C:\Blabla> . ./myfunction.ps1
C:\Blabla> Personal-Function my-principal-id
Hey ho my-principal-id
C:\Blabla> _
Note: Load with . .\myfunction.ps1
(note the two dots at the start) because you don’t want to execute the file in a sub-process, you want to load the file in this process.
.\myfunction.ps1
(which will “work”), a new sub-shell is spawned, which loads the file, does nothing, and ends.source
or .
command)(PS: Powershell syntax highlighting should work, but doesn’t. Uncool.)