PowerShell for Windows Admins

January 20, 2016  4:37 AM

WMF 4.0 Updates available downlevel

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The WMF 4.0 Updates that were released in November 2014 for Windows 8.1 and Windows Server 2012 R2 are now available for:

Windows Server 2012

Windows Server 2008 R2 SP1

Windows 7 SP1

You need WMF 4.0 installed to install the update

Details from http://blogs.msdn.com/b/powershell/archive/2016/01/19/windows-management-framework-wmf-4-0-update-now-available-for-windows-server-2012-windows-server-2008-r2-sp1-and-windows-7-sp1.aspx

January 18, 2016  1:44 PM

Setting external time source in AD

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

The PDC emaulator in the root domain of your AD forest should point to an external time source. For some odd reason the PDC emulator in my lab wasn’t doing that. Easily remedied:
## set external time source
## set server type to NTP
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters -Name Type -Value ‘NTP’
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name AnnounceFlags -Value 5
## Enable NTP server
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer -Name Enabled -Value 1
## Specify Time source
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters -Name NtpServer -Value ‘time.windows.com,0x1’
## Set poll interval in seconds – every 30 minutes
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient -Name SpecialPollInterval -Value 1800
## set max +/- time corrections in seconds – 24 hours
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name MaxPosPhaseCorrection -Value 86400
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name MaxnegPhaseCorrection -Value 86400

Stop-Service -Name W32Time
Start-Service -Name W32Time

Wait a minute or so and the time will be set correctly.

Not a job you have to do very often but having the code to do the job reduces the probability of errors

You can view the settings

Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters
Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config
Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer
Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient

January 17, 2016  2:27 PM

WMF 5.0

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

If you follow the PowerShell Team blog – http://blogs.msdn.com/b/powershell/ (and if you don’t you should) you’ll know that the WMF 5.0 RTM downloads were pulled just before Christmas. This was due to a bug that reset the module environment.

A comment on the blog – http://blogs.msdn.com/b/powershell/archive/2015/12/23/windows-management-framework-wmf-5-0-currently-removed-from-download-center.aspx – from the team indicates that it’s likely to be a few weeks before the bug is resolved and WMF 5.0 is available again for download.

January 16, 2016  8:40 AM

PowerShell conferences 2016

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

April is going to be a very busy month. It starts with the PowerShell plus DevOps Global Summit in Bellevue, Washington – https://eventloom.com/event/login/PSNA16

The PowerShell Conference EU 2016 follows later in the month – http://www.psconf.eu/

I’ll be speaking and running pre-conference workshops at both events. Hope to see you at one or the other.

January 12, 2016  4:45 AM

European PowerShell conference 2016

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

PowerShell Conference EU combines the former “Deutsche PowerShell Konferenz” and “PowerShell Summit EU” into one great big 3-day PowerShell event for Admins and DevOps in Europe and takes place April 20-22 in Hannover/Germany. With more than 40 international speakers including PowerShell inventor Jeffrey Snover, and more than 60 sessions, you are cordially invited to join this massive European PowerShell event. The agenda is up on www.psconf.eu, and registration is open. Seats and hotel capacity are limited so don’t wait and register!

Psconf.eu is partnering with powershell.org and psconf.asia, and together we want to ensure that you have a comprehensive local PowerShell conference in your region of the

January 11, 2016  10:34 AM

Testing against an arrays contents

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You may need to test if a value is a member of an array. PowerShell provides 2 operators for testing array membership  – –   -in and –contains.

Simple usage is like this

PS> $colours = ‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘indigo’, ‘violet’

PS> ‘blue’ -in $colours
PS> $colours -contains ‘blue’

Note the order of value and array changes between the operators.

You  can also use these operators in Where-Object

PS> $testcolours = ‘blue’, ‘pink’, ‘yellow’

PS> $testcolours | where {$_ -in $colours}
PS> $testcolours | where {$colours -contains $_}

Often the value we want is a property of an object

PS> $to = New-Object -TypeName PSObject -Property @{Colour = ‘green’}
PS> $to1 = New-Object -TypeName PSObject -Property @{Colour = ‘pink’}

PS> $to, $to1 | where Colour -in $colours

PS> $to, $to1 | where {$colours -contains $_.Colour}


The –in operator can be used in the simplified Where-Object syntax (it was introduced for that purpose) but –contains has to use the full, original syntax

For testing non-membership you also get –notin and –notcontains

PS> $to, $to1 | where Colour -notin $colours

PS> $to, $to1 | where {$colours -notcontains $_.Colour}


January 8, 2016  1:49 PM

WMI discovery

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

I’ve been working with System Center Configuration Manager this week. SCCM uses a lot of WMI. However, WMI discovery is still a bit of an art.

I needed to find the SMS_SCI_Component class.

If you know the namespace you can easily find the classes in that namespace but namespaces are hierarchical so you need to work down the tree.

The root namespace is at the top of the namespace tree so

C:\Scripts> Get-CimInstance -ClassName __NAMESPACE -Namespace root


After a bit of digging I found I needed the sms namespace

PS C:\Scripts> Get-CimInstance -ClassName __NAMESPACE -Namespace ‘root\sms’


which leads to:

Get-CimInstance -ClassName SMS_SCI_Component -Namespace ‘root\sms\site_PS1’

What if you only know the class?

There’s a little trick using Get-WmiObject

Get-WmiObject -Namespace root -Recurse -List -Class SMS_SCI_Component
NameSpace: ROOT\SMS\site_PS1


The trick is to start with the root namespace and use the –Recurse (search through all child namespaces) and –List (just give class details) parameters.  You’ll see a listing by namespace for each instance of the class discovered.

January 4, 2016  9:29 AM

Starting to use PowerShell

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A common question goes along the lines of  “I’ve leant PowerShell (from class, book etc) but what do I do next?”

The usual answer is to pick a problem in your organisation and solve it.

I’ve provided an example in this UK TechNet blog post


Its the second one of a series on PowerShell

January 2, 2016  10:34 AM

2015 December Scripting Games Puzzle

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The December 2015 puzzle was based on the 12 Days of Christmas song.  Starting with this here-string.

$list = @”
1 Partridge in a pear tree
2 Turtle Doves
3 French Hens
4 Calling Birds
5 Golden Rings
6 Geese a laying
7 Swans a swimming
8 Maids a milking
9 Ladies dancing
10 Lords a leaping
11 Pipers piping
12 Drummers drumming

A here-string is a multiline string. It is NOT an array – its a single string. The first task is to  split into an array of individual lines:

## split into individual lines
$s = $list -split ‘\n’

Each line of the multiline here string ends with a new line – so that becomes the split point.

The array can be sorted by length of line. Ascending or descending wasn’t specified so here’s both options:

‘Sort by length ascending’
$s | Sort-Object -Property Length

“`n ”
‘sort by length descending’
$s | Sort-Object -Property Length -Descending

Removing the numbers to give justthe text and sorting by length of text. I trmmed the strings as some empty spaces had appeared in the arrays. I think because I copied the here-string

“`n ”
‘remove numbers sort by length ascending’
$s.Trim() -replace ‘\d{1,2}\s’, ” | Sort-Object -Property Length #| group length | ft -a -wrap

“`n ”
‘remove numbers sort by length descending’
$s.Trim() -replace ‘\d{1,2}\s’, ” | Sort-Object -Property Length -Descending

Create objects. Split on white space and restrict output to 2 elements – number and text in this case. Create object using New-object

“`n ”
#’create objects’
$items = @()
$s.Trim() | foreach {
$item =  $psitem -split ‘\s’,2
$items += New-Object -TypeName PSObject -Property @{
Count = $item[0] -as [int]
Item = $item[1]

Count the number of birds

“`n ”
‘count of birds’
$birdcount = ($items -match ‘(Partridge|Doves|Hens|Birds|Geese|Swans)’ | Measure-Object -Property Count -Sum).Sum

Count all items

“`n ”
‘count of items’
$itemcount = ($items | Measure-Object -Property Count -Sum).Sum

If you treat the song as stating the gifts are cumulative then how many gifts are given in total.  Each item is given (13 – the day on which its given) times i.e. 12 to 1 times respectively.  The total number of items can be calculated like this

“`n ”
‘cumulative count of items’
$total = 0
$items | foreach {$total += $psitem.Count * (13-$psitem.Count) }

As a bonus here’s how you calculate the cumulative number of each type of item.

“`n ”
‘cumulative number of each item’
$totalitems =@()
$items | foreach {
$totalitems += New-Object -TypeName PSObject -Property @{
Count = $psitem.Count * (13-$psitem.Count)
Item = $psitem.Item

January 2, 2016  6:43 AM

Happy New Year 2016

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A new year and a number of things to which we can look forward.

The re-release of WMF 5.0. The WMF 5.0 download was pulled because a bug was over writing the module path. Look fot the download being available again soon.

Windows Server 2016 – Containers and Nano server give lots of new options for deployment and lots of new PowerShell to play with. There may even be other PowerShell changes – who knows given the new release processes.

In early April we have the PowerShell + DevOps Summit in Seattle. Late April sees the European PowerShell Conference in Hanover (tracks in German and English). Registration for both events is open.

The UK PowerShell group is reviving. After meetings in London and Manchester planning is underway for the next meetings – look for more info soon.

PowerShell in Action, third edition will appear.  Its already in early release and will complete this year.

A few other projects are in the pipeline which may or may not come to fruitition.

One prediction I can confidently make is that 2016 will NOT be the year that Unix/Linux conquers the desktop. 🙂

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: