Mastodon Mastodon - Powershell Script Base
 logo
  • Home 
  • Tags 
  • Blog posts 
  1. Home
  2. Blog posts
  3. Powershell Script Base

Powershell Script Base

Posted on March 23, 2021  (Last modified on July 11, 2024) • 1 min read • 133 words
Powershell  
Powershell  
Share via

Easy 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.

  • if you load it using .\myfunction.ps1 (which will “work”), a new sub-shell is spawned, which loads the file, does nothing, and ends.
  • so the currently active shell does now know about the function, cause it was only ever evaluated in the sub-shell which already quit
  • same as with *nix (the source or . command)

(PS: Powershell syntax highlighting should work, but doesn’t. Uncool.)

 Nextcloud out-of-memory PHP error
How to try Superset 
In case you want to follow me

Here are some links. The further to the right, the less active.

           
(c) Axel Bock | Powered by Hinode.
Link copied to clipboard
Code copied to clipboard