PowerShell for Windows Admins

March 18, 2016  6:11 AM

Network adapter Index

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell, WMI

A few years ago I wrote a post about setting the IP metric on a connection – https://richardspowershellblog.wordpress.com/2012/01/01/changing-ip-connection-metric/

I was recently asked if the Index’s associated with network adapters were consistent acros machines. The answer – unfortunately- is no they’re not as a rule.

I used the function from the original post

function Test-IPmetric {
Get-WmiObject -Class Win32_NetworkAdapter -Filter “AdapterType = ‘Ethernet 802.3′” |
foreach {
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter “Index=$($_.DeviceId)” |
Select-Object Description, Index, IPEnabled, IPConnectionMetric

to view the index

The Index property from Win32_NetworkAdapaterConfiguration is identical to the DeviceId from the Win32_NetworkAdapater class for a given network adapter.

if you try the function on a number of machines you’ll see that Index numbers assigned by Windows aren’t consistent.  My test VMs (Hyper-V) all seem to have the same index numbers assigned but I always create my VMs and add network adapters in the same way.

I wouldn’t rely on a particular index being assigned to a given network adapter – always pull the data and test

March 10, 2016  12:13 PM


Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Been testing things for my container talk at the Summit and opened Internet Explorer on my Server 2012 R2 machine to be greeted by

“Microsoft recommends upgrading to Windows 10”

Forget Server 2016 guys all you need is Windows 10  🙂

March 9, 2016  12:09 PM

PowerShell package management on PowerShell 3 and 4

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

PowerShell 5.0 introduced PackageManagement (Oneget) – for managing software installation packages and PowerShellGet for managing PowerShell modules.

Those features are now available on PowerShell 3 and 4


March 6, 2016  9:28 AM

PowerShell Summit 2016–sold out

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The last place for the PowerShell Summit has been sold. We’re at capacity so no more places can be made available.

Looking forward to seeing you all there

March 1, 2016  1:56 PM

Comparing lists

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

An interesting question on the forum regarding how you compare the contents of 2 collections.  The question revolved around comparing the contents of the Machine property of



$Machine = @(
‘System\CurrentControlSet\Control\Server Applications’,
‘Software\Microsoft\Windows NT\CurrentVersion’

Start by getting the data from the registry

$p = Get-ItemProperty -Path ‘HKLM:\System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedExactpaths’ -Name Machine

System\CurrentControlSet\Control\Server Applications
Software\Microsoft\Windows NT\CurrentVersion

So the 2 look identical but how do we do that programmatically.

You can’t do

$machine -eq $p.machine

you get nothing back.

You have to use Compare-Object:

Compare-Object $machine $p.machine

You’ll get nothing back so check  everything is equal:

PS> Compare-Object $machine $p.machine -IncludeEqual

InputObject                                                                     SideIndicator
———–                                                                         ————-
System\CurrentControlSet\Control\ProductOptions        ==
System\CurrentControlSet\Control\Server Applications ==
Software\Microsoft\Windows NT\CurrentVersion            ==

Compare-Object returns nothing if the 2 objects match so your comparison ends up as

PS> if ( -not (Compare-Object $machine $p.machine)){‘yay’}else{‘nay’}

You can test it works

$m2 = ‘item1’, ‘item2’, ‘item3’

PS> Compare-Object $machine $m2

InputObject                                          SideIndicator
———–                                          ————-
item1                                                =>
item2                                                =>
item3                                                =>
System\CurrentControlSet\Control\ProductOptions      <=
System\CurrentControlSet\Control\Server Applications <=
Software\Microsoft\Windows NT\CurrentVersion         <=

PS> if ( -not (Compare-Object $machine $m2)){‘yay’}else{‘nay’}

February 29, 2016  6:30 AM

CSV file with [] in headers

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

With the PowerShell Summit registration deadline rapidly approaching I wanted to see how registrations were going. I can down load a CSV file from the event website that lists attendees.

Great. Just need to do some sorting and grouping and I’m golden.

I’m running PowerShell 5.0 on Windows 10 Insider build 14271

First off I discovered that the headers of the CSV file contain [] i.e. they look like this for example:


Sorting by attendee type

Import-Csv -Path C:\test1\AttendeeReport.csv |
Sort-Object -Property [AttendeeType]

works but if I add a check for complete registrations

Import-Csv -Path C:\test1\AttendeeReport.csv |
Where-Object [REGISTRATIONSTATUS] -eq ‘Complete’|
Sort-Object -Property [AttendeeType]

I get nothing back.

Import-Csv -Path C:\test1\AttendeeReport.csv |
Where-Object ‘[REGISTRATIONSTATUS]’ -eq ‘Complete’|
Sort-Object -Property [AttendeeType]

Is no better. You need to revert to old style where-object syntax

Import-Csv -Path C:\test1\AttendeeReport.csv |
Where-Object {$_.'[REGISTRATIONSTATUS]’ -eq ‘Complete’}|
Sort-Object -Property [AttendeeType]

Now I want to group on attendee type

PS> Import-Csv -Path C:\test1\AttendeeReport.csv |
>> Where-Object {$_.'[REGISTRATIONSTATUS]’ -eq ‘Complete’}|
>> Sort-Object -Property [AttendeeType] |
>> Group-Object -Property [AttendeeType] -NoElement
Group-Object : Wildcard characters are not allowed in “[AttendeeType]”.
At line:4 char:1
+ Group-Object -Property [AttendeeType] -NoElement
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Group-Object], NotSupportedException
+ FullyQualifiedErrorId : ExpressionGlobbing2,Microsoft.PowerShell.Commands.GroupObjectCommand

This partially works

Import-Csv -Path C:\test1\AttendeeReport.csv |
Where-Object {$_.'[REGISTRATIONSTATUS]’ -eq ‘Complete’}|
Sort-Object -Property [AttendeeType] |
Group-Object -Property ‘`[AttendeeType`]’ -NoElement

But just gives me a count of the total number of attendees.

I tried a number of ways of dealing with the [] in the headers and therefore in the property names. In the end I decided it was going to be easier to completely reset the headers in the csv file:

Import-Csv -Path C:\test1\AttendeeReport.csv -Header  ‘UserName’, ‘REGISTRATIONSTATUS’, ‘AttendeeType’, ‘ConfirmationCode’, ‘PaymentAuthorization’, ‘RegisteredByEmail’, ‘PromotionCode’, ‘BioName’, ‘BioTitle’, ‘BioEmail’, ‘Created’, ‘Updated’, ‘LastName’, ‘Dietary’, ‘Alumni’, ‘FirstName’, ‘Twitter’, ‘BestEmail’

This means I have to skip the first record because it looks like this:

UserName             : [UserName]
AttendeeType         : [AttendeeType]
ConfirmationCode     : [ConfirmationCode]
PaymentAuthorization : [PaymentAuthorization]
RegisteredByEmail    : [RegisteredByEmail]
PromotionCode        : [PromotionCode]
BioName              : [BioName]
BioTitle             : [BioTitle]
BioEmail             : [BioEmail]
Created              : [Created]
Updated              : [Updated]
LastName             : [LastName]
Dietary              : [Dietary]
Alumni               : [Alumni]
FirstName            : [FirstName]
Twitter              : [Twitter]
BestEmail            : [BestEmail]

My grouping script is now much simpler

Import-Csv -Path C:\test1\AttendeeReport.csv -Header  ‘UserName’, ‘REGISTRATIONSTATUS’, ‘AttendeeType’, ‘ConfirmationCode’, ‘PaymentAuthorization’, ‘RegisteredByEmail’, ‘PromotionCode’, ‘BioName’, ‘BioTitle’, ‘BioEmail’, ‘Created’, ‘Updated’, ‘LastName’, ‘Dietary’, ‘Alumni’, ‘FirstName’, ‘Twitter’, ‘BestEmail’ |
Select-Object -Skip 1 |
Where-Object REGISTRATIONSTATUS -eq ‘Complete’|
Sort-Object -Property AttendeeType |
Group-Object -Property AttendeeType –NoElement

If I come across other CSV files with [] in the headers I’m going to go for immediate replacement of the headers as the way to get the job done.



February 27, 2016  6:45 AM

PowerShell Summit–Registration closing soon

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Registration for the PowerShell and DevOps Global Summit will be closing in the next few days – http://powershell.org/wp/2016/02/18/last-chance-for-powershellsummit-org-registration/

We still have a few places left but you’ll have to hurry

February 24, 2016  3:22 PM

WMF 5.0 RTM has been republished

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A post on the PowerShell Team blog – https://blogs.msdn.microsoft.com/powershell/2016/02/24/windows-management-framework-wmf-5-0-rtm-packages-has-been-republished/ – reports that WMF 5.0 RTM has been republished.

You’ll have to uninstall the original RTM package before installing the new one.

February 24, 2016  1:14 PM

Get-ADUser quirk

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

Came across an interesting quirk of the way Get-ADUser works.


If you use the –Identity parameter and tell it to find a specific user

PS> Get-ADUser -Identity dontexist
Get-ADUser : Cannot find an object with identity: ‘dontexist’ under: ‘DC=Manticore,DC=org’.
At line:1 char:1
+ Get-ADUser -Identity dontexist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (dontexist:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser


You get an error if the user can’t be found


If you use a –Filter to perform the same search – its quite OK for nothing to be returned

PS> Get-ADUser -Filter {SamAccountName -eq ‘dontexist’}


Something to be aware of when error handling. When using the filter you can’t use try-catch because there’s no error if there’s no result

February 22, 2016  11:18 AM

IP Default Gateways by cmdlet

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Network Adapter, Powershell

Following my recent post on setting the default gateway by using the Win32_NetworkAdapterConfiguration CIM class here’s how you do it using the networking cmdlets

Discover your adapters


Check the default gateway for an adapter

Get-NetIPConfiguration -InterfaceIndex 12 | select InterfaceIndex, IPv4Address, IPv4DefaultGateway

Set the default gateway

New-NetRoute -InterfaceIndex 12  -DestinationPrefix ‘’ -NextHop ‘’

This isn’t as intuitive as using the CIM class

Check the setting

£> Get-NetIPConfiguration -InterfaceIndex 12
InterfaceAlias       : Ethernet
InterfaceIndex       : 12
InterfaceDescription : Microsoft Hyper-V Network Adapter
NetProfile.Name      : Manticore.org
IPv4Address          :
IPv6DefaultGateway   :
IPv4DefaultGateway   :
DNSServer            :

To remove the default gateway

Remove-NetRoute -InterfaceIndex 12 -NextHop ‘’ -Confirm:$false

If you leave off –Confirm you’ll be prompted to confirm the action on the active and persistent stores i.e. twice.

All of the above cmdlets are part of the NetTCPIP module available on Windows 8/Server 2012 and later.


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: