Nice to know – Basic PowerShell building blocks to create scripts

When using PowerShell you will after a while create a small collection of “stuff you can user than one time snippets” and as a speaker I use them as samples. During one of the 60 minutes of OSD with Johan and Mike I did a promise, to publish them, so here it is, a very basic but practical collection of snippets to use in your scripts.

Step One: Doing the “Get”

Using get is SAFE and a very practical way to start using powershell, I mean just finding the things you need to change/manage is “handy”, in this case, we use Get-VM to get all VMs in Hyper-V, but what we really need is to start the once not running, or something like that.

First line will get me ALL VMs, Second will get all that is off, third will give the same, but it will exclude the VM called TEST, fourth will do the same but also start all of them. Compare that line to “click-like-a-maniac” in Hyper-V manager…

Get-VM | Where-Object -Property State -EQ -Value "Off" 
Get-VM | Where-Object -Property State -EQ -Value "Off" | Where-Object -Property Name -NE "TEST"| 
Get-VM | Where-Object -Property State -EQ -Value "Off" | Where-Object -Property Name -NE "TEST"| Start-VM

Step Two: Using parameters as input in your script

The Param bloc can be used to make a script act like a cmdlet. We will present some questions that we the “convert” into variables that can be used in the script:

Note: The Param block needs to be in the beginning of the script to work…

The first Param will ask for the name, and that is mandatory so we really need to fill that out, also the name needs to be at least 3 characters long, but not longer then 20 characters

The second param is the same, but that will be stored in $lastname instead of $firstname

The Third is a bit different, first it is not mandatory, second it has a default value and third it only allows Sales, Store or Management as valid options. Because we do that we can also tab thoose values on the command line when we run the script which is very nice

[parameter(mandatory=$True,HelpMessage="First Name")] 

[parameter(mandatory=$true,HelpMessage="Last Name")] 

$Department = "Store" 


Step Three: If’s and But’s

Same story, but I just added a “if” and a “switch” block here. The If will check if you work at sales and the switch will check all different countries for various information. If I need an exclusion, I find If to the easiest way, but if there is a bunch of different scenarios, the switch is easier to use.

[parameter(mandatory=$True,HelpMessage="First Name")] 

[parameter(mandatory=$true,HelpMessage="Last Name")] 




If($Department -like "Sales") 
  {Write-Host "Sales is boring"} 
  {Write-Host "You are working in $Department"}

Write-Host "Contact Johan for salary discussions" 
Write-Host "Contact Harold for salary discussions" 
Write-Host "Contact Linus for salary discussions" 
Default{Write-Host "Epic fail..." 

Step Four: Reading data from XML file instead of typing everything on the command line…

Even if its nice to have all on the command line and using param, you will soon find that typing 192,168.1.1 as the default gateway every time you build a new VM using PowerShell is tidies (or boring), therefore you can store parameters that seldom change in a an XML file and read in the data like this:

$SettingsFile = ".\XMLSettings.xml" 
[xml]$Settings = Get-Content $SettingsFile -Verbose

$name = $Settings.Settings.Defaults.Name 
Write-Host $name


From the file that looks like this:


As you can see, it is rather easy.

Step Five: Where am i?

Wrinting scripts is fun, but after a while you realize that hardcoding location is kind of bad, especially when you need to move the script and the data with it and of course there is a solution for that, just use the builtin variable called $MyInvocation and Split-Path to get the location script is running from and/or the name of the script it self.

$RunningFromFolder = $MyInvocation.MyCommand.Path | Split-Path -Parent 
Write-Host “Running this from $RunningFromFolder”

$ScriptName = $MyInvocation.MyCommand.Path | Split-Path –Leaf 
Write-Host “I can feel my name is…$ScriptName”


Categories: PowerShell

Tagged as:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.