PowerShell for Windows Admins

April 29, 2014  7:20 AM

PowerShell Summit NA 2014–day 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Day one of the Summit has been and gone and it has lived up to expectations. With more attendees and more sessions than last year this is the place to be if you want your PowerShell questions answered.

One of the highlights of day 1 was Steve Muraski’s sessions on DSC. He has recorded them and will be making them available – I’ll post the link when available.

April 25, 2014  3:49 PM

To the Summit

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Off to the PowerShell Summit tomorrow morning – 3 days of wall to wall PowerShell with some of the most knowledgeable people in the world.  Its going to be fun.

More details of the European Summit will be coming soon – remember its 20 September – 1 October in Amsterdam

April 21, 2014  10:40 AM

Working with Server Core–Domain join

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Windows Server can be installed in two ways – full fat GUI or Server Core.  The latter is Windows without Windows.  The GUI components are stripped off and you’re just left with the core components.  This results in a smaller server – I’ve got 2 machines running in my Hyper-V test environment. Both are set to use dynamic memory with 512MB startup RAM. The full GUI machine needs 1126MB while the Server Core version needs 566MB.

With Server Core all you get is a prompt for administration – unfortunately its cmd.exe but typing powershell starts PowerShell – including running a profile.

Without a GUI you need to use the command line to do everything – I’ll be doing my demos at the PowerShell Summit from Server Core machines so some things will have to wait until after then – and I’m going to do a series on posts on administering Server Core machines.

I’ve already shown you  how to test if your machine is activated.  This how you join it to the domain.

Make sure that the IP and DNS server addresses have been set so the machine can find a domain controller.

$cred = Get-Credential

Add-Computer -Credential $cred -DomainName sphinx -Restart

Create a credential for the account that can join the machine to the domain.  use Add-Computer and supply the credential and domain name. The –ReStart parameter forces a restart post domain join.

if you want to see the results of calling Add-Computer then drop the restart switch and use Restart-Computer whenever you want the restart to happen

April 19, 2014  7:52 AM

Subnets and prefixes

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Sounds a bit like an old time role playing game but is actually a useful piece of knowledge.

You can define a subnet mask in 2 ways. Either use the full mask e.g. or define the number of bits in the mask e.gg 21 which is known as the prefixlength in the PowerShell networking cmdlets.

But can you relate a full subnet mask to the number of bits. Some are obvious but the others I need to work out.

Time for a quick PowerShell function:

function show-subnetmask{

foreach ($prefixlength in 8..30) {

switch ($prefixlength){

{$_ -gt 24}


$bin = (‘1’ * ($prefixlength – 24)).PadRight(8, ‘0’)

$o4 = [convert]::ToInt32($bin.Trim(),2)

$mask = “255.255.255.$o4”



{$_ -eq 24}


$mask = ‘’



{$_ -gt 16 -and $_ -lt 24}


$bin = (‘1’ * ($prefixlength – 16)).PadRight(8, ‘0’)

$o3 = [convert]::ToInt32($bin.Trim(),2)

$mask = “255.255.$o3.0”



{$_ -eq 16}


$mask = ‘’



{$_ -gt 8 -and $_ -lt 16}


$bin = (‘1’ * ($prefixlength – 8)).PadRight(8, ‘0’)

$o2 = [convert]::ToInt32($bin.Trim(),2)

$mask = “255.$o2.0.0”



{$_ -eq 8}


$mask = ‘’





$mask = ‘’



New-Object -TypeName psobject -Property @{

PrefixLength = $prefixlength

Subnetmask = $mask




Most people will be using subnets between 8 and 30 bits in length so start with that range and for each value work through the switch statement. If the value is 8,16 or 24 the subnet mask can be set directly. Otherwise it needs to be calculated. The calculations are the same – the difference is which octet of the subnet mask is affected.

For instance if the prefix length is between 16 and 24 (exclusive)

$bin = (‘1’ * ($prefixlength – 16)).PadRight(8, ‘0’)

$o3 = [convert]::ToInt32($bin.Trim(),2)

$mask = “255.255.$o3.0”


Convert the number to a binary representation – the amount you need to subtract depends on the octet with which you are working

Convert the binary to an integer and insert into the subnet mask string.

An object is output that has the subnet mask and prefix length as properties.

Put the function in a module on your module path and you’ll be able to use it as a quick lookup when you need to convert subnet masks to prefix lengths or vice versa

April 16, 2014  11:30 AM

WMI against remote machines

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

WMI is a great tool for managing your Windows machines – I’d argue that PowerShell wouldn’t be as powerful as it is without WMI. If you question that remember that 60% of the additional cmdlets in Windows Server 2012 & 2012 R2 are CDXML based i.e. publish a WMI class as a PowerShell module.

PowerShell 2.0 introduced a suite of WMI cmdlets:






PowerShell 3.0 introduced the CIM cmdlets:













So which should you use?

There are a number of differences.

The WMI cmdlets return live objects and the CIM cmdlets return inert objects. This isn’t too much of an issue if you use Invoke-CimMethod. I’d also recommend using Invoke-WMImethod over creating an object and calling the method on that.

The real difference is in the protocol used to access remote machines. The WMI cmdlets use DCOM and the CIM cmdlets default to WSMAN. At this point you may be thinking that you can just use the CIM cmdlets but the remote machine must be running WSMAN 3.0 which comes with PowerShell 3.0 or 4.0.. The CIM cmdlets can’t connect to WSMAN 2.0 which is the PowerShell 2.0 version.

At that point you have to use a CIM session that drops back to DCOM or run Get-WMIobject through a PowerShell remoting session.

Overall the CIM cmdlets win – especially when you consider Get-CimClass and Get-CimAssociatedInstance

April 15, 2014  10:06 AM

PowerShell versions

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve been using the CIM cmdlets for a number of posts recently and had a comment that a reader got a message that Get-CimInstance didn’t exist on their Windows 7 machine.

Windows 7 ships with PowerShell 2.0; Windows 8 with PowerShell 3.0 and Windows 8.1 with PowerShell 4.0.

You need PowerShell 3.0 or 4.0 to have the CIM cmdlets.

You can install PowerShell 3.0 or 4.0 on Windows 7. You need to go to the Microsoft download site, download and install the appropriate version of Windows Management Framework (WMF 3 contains PowerShell 3.0 and WMF 4 contains PowerShell 4.0).

You can tell which version of PowerShell you are running by looking in the $psversiontable automatic variable:

£> $psversiontable

Name Value

—- —–

PSVersion 4.0

WSManStackVersion 3.0


CLRVersion 4.0.30319.34014

BuildVersion 6.3.9600.16394

PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}

PSRemotingProtocolVersion 2.2

April 14, 2014  2:43 PM

Status of Office software

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You can also use the SoftwareLicensingProduct CIM class to test the status of your Office products.

Get-CimInstance -ClassName SoftwareLicensingProduct -Filter “Name LIKE ‘Office%'” |

where PartialProductKey |

select Name, ApplicationId, LicenseStatus

You need to be careful with Office as you might find a lot more options than you expected. On my machine I found this:

Get-CimInstance -ClassName SoftwareLicensingProduct -Filter “Name LIKE ‘Office%'” | select Name -Unique | sort name

Office 15, OfficeO365ProPlusR_Grace edition

Office 15, OfficeO365ProPlusR_Retail edition

Office 15, OfficeO365ProPlusR_Subscription1 edition

Office 15, OfficeO365ProPlusR_Subscription2 edition

Office 15, OfficeO365ProPlusR_Subscription3 edition

Office 15, OfficeO365ProPlusR_Subscription4 edition

Office 15, OfficeO365ProPlusR_Subscription5 edition

Office 15, OfficeO365ProPlusR_SubTrial1 edition

Office 15, OfficeO365ProPlusR_SubTrial2 edition

Office 15, OfficeO365ProPlusR_SubTrial3 edition

Office 15, OfficeO365ProPlusR_SubTrial4 edition

Office 15, OfficeO365ProPlusR_SubTrial5 edition

Office 15, OfficeO365SmallBusPremR_Grace edition

Office 15, OfficeO365SmallBusPremR_Retail edition

Office 15, OfficeO365SmallBusPremR_Subscription1 edition

Office 15, OfficeO365SmallBusPremR_Subscription2 edition

Office 15, OfficeO365SmallBusPremR_Subscription3 edition

Office 15, OfficeO365SmallBusPremR_Subscription4 edition

Office 15, OfficeO365SmallBusPremR_Subscription5 edition

Office 15, OfficeO365SmallBusPremR_SubTrial1 edition

Office 15, OfficeO365SmallBusPremR_SubTrial2 edition

Office 15, OfficeO365SmallBusPremR_SubTrial3 edition

Office 15, OfficeO365SmallBusPremR_SubTrial4 edition

Office 15, OfficeO365SmallBusPremR_SubTrial5 edition

Office 15, OfficeProjectProCO365R_Subscription edition

Office 15, OfficeProjectProCO365R_SubTest edition

Office 15, OfficeProjectProCO365R_SubTrial edition

Office 15, OfficeProjectProDemoR_BypassTrial180 edition

Office 15, OfficeProjectProMSDNR_Retail edition

Office 15, OfficeProjectProO365R_Subscription edition

Office 15, OfficeProjectProO365R_SubTest edition

Office 15, OfficeProjectProO365R_SubTrial edition

Office 15, OfficeProjectProR_Grace edition

Office 15, OfficeProjectProR_OEM_Perp edition

Office 15, OfficeProjectProR_Retail edition

Office 15, OfficeProjectProR_Trial edition

Office 15, OfficeProPlusDemoR_BypassTrial180 edition

Office 15, OfficeProPlusMSDNR_Retail edition

Office 15, OfficeProPlusR_Grace edition

Office 15, OfficeProPlusR_OEM_Perp edition

Office 15, OfficeProPlusR_Retail edition

Office 15, OfficeProPlusR_Trial edition

Office 15, OfficeVisioProCO365R_Subscription edition

Office 15, OfficeVisioProCO365R_SubTest edition

Office 15, OfficeVisioProCO365R_SubTrial edition

Office 15, OfficeVisioProDemoR_BypassTrial180 edition

Office 15, OfficeVisioProMSDNR_Retail edition

Office 15, OfficeVisioProO365R_Subscription edition

Office 15, OfficeVisioProO365R_SubTest edition

Office 15, OfficeVisioProO365R_SubTrial edition

Office 15, OfficeVisioProR_Grace edition

Office 15, OfficeVisioProR_OEM_Perp edition

Office 15, OfficeVisioProR_Retail edition

Office 15, OfficeVisioProR_Trial edition

which was a lot more than I expected.

It is possible to use WMI to set the product key – use the SoftwareLicensingService class

April 13, 2014  5:50 AM

Checking license activation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’m building some virtual machines for my demo’s at the upcoming PowerShell summit. To make the demo’s, and setup, more interesting(?) I decided to use some Server Core instances.

The usual setup activities become a bit more interesting with Server Core – particular Windows activation.

Windows 2012 R2 will activate itself if the new machine has an Internet connection when it is created. With the GUI version of Windows you can check that Windows is activated using the System applet in Control Panel.

If you’re using Server Core you can use WMI to test activation:

Get-CimInstance -ClassName SoftwareLicensingProduct |

where PartialProductKey |

select Name, ApplicationId, LicenseStatus |

Format-List *

Use the SoftwareLicensingProduct WMI class and filter for PartialProductkey – that means a product key has been entered. You can then select the name of the product the ApplicationId and the LicenseStatus:

Name : Windows(R), ServerStandard edition

ApplicationId : 55c92734-d682-4d71-983e-d6ec3f16059f

LicenseStatus : 1

A License status of 1 indicates that its licensed – i.e. activated

More on using WMI to test and set activation in chapter 13 of PowerShell and WMI – www.manning.com/siddaway2

April 11, 2014  2:17 AM

PowerShell Deep Dive and Save the Children

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I co-edited PowerShell Deep Dives – http://www.manning.com/hicks/ – alongside Jeff Hicks and other PowerShell MVPs.  The book is collection of chapters from  PowerShell experts from around the world.

The list of authors includes:

Jeffery Hicks, Richard Siddaway, Oisín Grehan, Aleksandar Nikolić, Chris Bellée, Bartek Bielawski, Robert C. Cain, Jim Christopher, Adam Driscoll, Josh Gavant, Jason Helmick, Don Jones, Ashley McGlone, Jonathan Medd, Ben Miller, James O’Neill, Arnaud Petitjean, Vadims Podans, Karl Prosser, Boe Prox, Matthew Reynolds, Mike Robbins, Donabel Santos, Will Steele, Trevor Sullivan, and Jeff Wouters.

Best of all the royalties from the book all go to Save the Children.  The more copies we sell the more they receive.  If you haven’t bought a copy please do so. Available from your favourite bookshop or direct from the publisher.

April 10, 2014  2:16 PM


Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A comment was left on my last post stating that the requires keyword could be used to test for modules.

Requires is a keyword that can be put at the top of scripts and modules. It will prevent the script or module running if the requirement isn’t met.  You can test for a number of items. This list is for  PowerShell 4.0.  earlier versions of PowerShell have fewer options.

PowerShell version:

#Requires –version 3

This means that the code will only run on PowerShell version 3 or later

PowerShell snapin

#Requires –PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Loads the Exchange snapin.  iIf its not available the script won’t continue.


#Requires -Modules PSWorkflow, @{ModuleName=”PSScheduledJob”;ModuleVersion=}

Use the module name or a hash table with name, version and optionally the GUID for the module. If the required module can’t be loaded the script fails. This is different to my test-module function as I was only interested in discovery – I wasn’t actually using the test. If your script requires a module use the #Requires statement

Elevated privileges

#Requires –RunAsAdministrator

If PowerShell isn’t running with elevated privileges the script terminates.


#Requires –ShellId Microsoft.PowerShell

This uses the default PowerShell Shell.  Note that the console and ISE both return Microsoft.PowerShell    when you test $shellid.  If you want to test for console vs ISE use

£> $host.name


£> $Host.Name

Windows PowerShell ISE Host

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: