Terms of Use For FixedByVonnie

By proceeding to access fixedByVonnie.com, you expressly acknowledge, and agree to, all of the following:

fixedByVonnie.com is a personal website and blog owned by Security Plus Pro LLC, which is being presented for informational purposes only. The views on this website are solely those of the website owner (and not those of any employer or of any professional associations affiliated with the website owner).  Any views expressed in this website and any information presented on this website, or in any of its blog entries, should not be relied on for any purpose whatsoever other than as the personal opinions of the website owner.  The website owner expressly disclaims any and all liability for any information presented on this site.  The owner of this website and its blog posts shall not be held liable, and shall be held harmless, for any errors or omissions in any information or representations contained in this website, or in any of its blog entries.  The website owner also expressly disclaims any liability for the current or future availability of any such information. The website owner makes no representations as to the accuracy or completeness of any information on this website or which may be found by following any link on this website. The website owner shall not be held liable for any losses, injuries, damages, claims, or causes of action, from the display or use of any information on this website or in any of its blog entries. If you use the information on this website, or on any of its blog entries, you do so solely at your own risk.

Say what’s up to PowerShell 5 (Part 26/27) - fixedByVonnie

Say what’s up to PowerShell 5 (Part 26/27)

When you start to get into scripting you’re going to want to parameterize your scripts because it makes it reusable and professional (not to mention respectable).

In this guide I’m going to show you how to touch up your scripts to make them easier to read.   As a rule of thumb, you should never use aliases in your scripts and you should break long lines at commas and pipes.

Technically you could also use a back-tick to break the line but it’s kind of hard to see those back-ticks (even with the font size cranked up) so I stay far from the ticks.

Let’s quickly build a command that gets some disk information from a few computers.

We’ll use Get-CimInstance (which I talked about in the WIM, CIM tutorial)

Get-CimInstance -ClassName Win32_logicaldisk -filter "DeviceID='c:'" -ComputerName dc16alx1

Get-CimInstance scripting

Let’s use Get-Member to see what properties we want:

Get-CimInstance -ClassName Win32_logicaldisk -filter "DeviceID='c:'" -ComputerName dc16alx1  | gm

Get-Member is cool

Okay lets check on the computer hostname and the amount of freespace.

Get-CimInstance -ClassName Win32_logicaldisk -filter "DeviceID='c:'" -ComputerName dc16alx1  | Select-Object PSComputerName, Freespace

Get-CimInstance Select-Object

Okay but let’s change the column from PSComputerName to Host.  PSComputerName is a pretty ugly column name lol.

Get-CimInstance -ClassName Win32_logicaldisk -filter "DeviceID='c:'" -ComputerName dc16alx1  | Select-Object @{n="Host";e={$_.PSComputerName}}, Freespace

Hash Tables

And what the heck, let’s convert the Freespace into gigs so it’s easier to read:

Get-CimInstance -ClassName Win32_logicaldisk -filter "DeviceID='c:'" -ComputerName dc16alx1  | Select-Object -Property @{n="Host";e={$_.PSComputerName}}, @{n="Free GBs";e={$_.FreeSpace / 1gb -as [int]}}

Hash Table 2

Awesome. I also added -Property after Select-Object to make it really clear what I’m doing.

Now let’s plop this in the ISE and make it easier to read.  Remember we never want to type this again that’s why we’re putting it in a script.

We’ll break the line on pipe and comma characters so we don’t have to scroll all the way to the right to read it.

The pimped out ISE

Alright press F5 and run the script.

Running the Script

Cool, so it runs now we just need to make it reusable so that people don’t have to open our script and change it.  We want it to run without being modified.

Parameterizing the Script

Press Ctrl + r in the ISE to make sure you’re in the script view then press Ctrl + s and save the script as harddrive_data.ps1.

Now let’s take a step back for moment and look at the parameters.  Right now this is a working script but it’s not finshed.

As you can see, the dc16alx1 hostname is hard coded into the script.  This isn’t smart since people are going to want to run your script against other hosts.  So we need to do a little work here:

Type this on the first line of your script:


$computerName = 'dc16alx1'


Param 1

then in your script replace the hard coded value of dc16alx1 with $computerName. Save it and rerun.

Variables and Parameters

So now when we run the script we can specify a -computerName or if we don’t specify one it’ll take dc16alx1 by default.

.\harddrive_data.ps1 -Comp

hit tab and watch it auto complete to -computerName and then add in your hostname.

You should also specify that the computer name is a string and can take multiple values.  I mean, you want to use this script against multiple computers right? Check this out:


[string[]]$computerName = 'dc16alx1'


[string] says the data type is a string and the double bracket after string[ ] means we want it to accept a comma separated list of strings.  So we can pass a list of hostnames to the script and it will run.

Here’s something else we can do.  We can make it so that that computer name is a mandatory parameter.

If you think about it, there’s no reason to run this script unless you have a computer so by making -computerName mandatory it forces the user to enter one.





Mandatory parameters

In the last post in this series 27 part series we’ll close up with Comment-Based help and where you can go to really sharpen your PowerShell skills…


Connect with Vonnie on Twitter

Posted in Windows, Windows 10 Tagged with: