PowerShell for Windows Admins

June 9, 2016  3:47 AM

Converting strings to dates

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You’ll see many examples of this:

PS>  [datetime]’12/25/2016′

25 December 2016 00:00:00

This works great if the date is in US format – MM/DD/YYYY

For those of us who use different date formats – such as England DD/MM/YYYY – this approach won’t work

PS>  [datetime]’25/12/2016′
Cannot convert value “25/12/2016” to type “System.DateTime”. Error: “String was not recognized as a
valid DateTime.”
At line:1 char:1
+ [datetime]’25/12/2016′
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

You need to rearrange the string  –  for example

$ds = ’25/12/2016′ -split ‘/’

PS>  [datetime](“{0}/{1}/{2}” -f $ds[1], $ds[0], $ds[2])

25 December 2016 00:00:00

PS>  Get-Date -Day $ds[0] -Month $ds[1] -Year $ds[2]

25 December 2016 10:44:45

or even simpler

PS>  Get-Date -Date ’25/12/2016′

25 December 2016 00:00:00

Not sure when the last option came in. Its in PowerShell v5

June 8, 2016  5:13 AM

32 or 64 and/or Administrator

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

When you run the PowerShell console (or ISE) the default icon runs a 32 or 64 bit version that matches your OS. On a 64 bit machine you have the option of running in 32bit (icons have a (x86) suffix on the title.

How can you tell whether you’re running in 32 or 64 bit mode?

One way is shown in this forum question – http://powershell.org/forums/topic/requires/#post-42226

I prefer the simpler:

if ([System.IntPtr]::Size -eq 8) {$size = ’64 bit’}
else {$size = ’32 bit’}

I don’t like automatically kicking into the required bit version if its wrong. I prefer to abort the processing with a message to run as 32 or 64 bit as appropriate

You can perform a similar test for administrator privileges (running elevated)

$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$secprin = New-Object Security.Principal.WindowsPrincipal $currentUser
if ($secprin.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{$admin = ‘Administrator’}
else {$admin = ‘non-Administrator’}

Though on later versions of Powershell its easier to use

#requires –RunAsAdministrator


June 7, 2016  4:03 AM

Local Administrators

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Finding the local administrators on a system is a not infrequent action.  There are a number of ways to do this.

The oldest method is to use the ADSI WinNT provider

$group =[ADSI]”WinNT://$($env:COMPUTERNAME)/Administrators, group”
$members = @($group.psbase.Invoke(“Members”))
$members | Foreach {$_.GetType().InvokeMember(“Name”, ‘GetProperty’, $null, $_, $null)}

NOTE – this doesn’t work on my Windows 10 system – build 14352

I’d recommend avoiding the WinNT provider if you can

WMI provides this option

$group = Get-CimInstance -ClassName Win32_Group -Filter “Name=’Administrators'”
Get-CimAssociatedInstance -InputObject $group -ResultClassName Win32_UserAccount

You can also use a .NET based approach with the System.DirectoryServices.AccountManagement  namespace

using assembly System.DirectoryServices.AccountManagement
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $($env:COMPUTERNAME)

$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::Name
$grp = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, “Administrators”)
$grp.Members | select SamAccountName

This is a bit more complicated as you have to load the assembly (using is new to PowerShell v5 – use Add-Type in earlier versions)

Set the context to the local machine and the identity type to Name

You can then use FindByIdentity() to get the local adminsitrators groups and look at the Members property to find the group members.

PowerShell v5 brings a Local Accounts module – Microsoft.PowerShell.LocalAccounts


NOTE – depending on your version of PowerShell v5 you may, or may not have this module. Its present in the later Windows 10 builds (on Insider Preview) and in Windows server 2016 TP 5. Eventually it’ll become available on all Windows 10 systems through Windows updates.

PS>  Get-LocalGroupMember -Group Administrators | select Name


June 6, 2016  3:39 AM

PowerShell Summit 2017–request for topics

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The PowerShell Summit is a community event. PowerShell.orh may organise it but we’re very aware that it is the PowerShell community’s event.

We’ve been planning the  2017 Summit for a while and we’ve reached a point in the process where we need your help.

What topics would you like to see covered at the 2017 Summit. We’ve published a request for topics at –http://powershell.org/request-for-topics/

Please let us know the topic areas you’d like covered and we can build that information into our agenda planning to ensure we have the Summit the community wants

June 1, 2016  1:54 PM

Dates in file and folder

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

If you want to incorporate the date in a file or folder name you can’t use Get-Date directly

PS>  Get-Date

01 June 2016 20:52:03

The simplest answer is to use the –Format or –Uformat parameters:

PS>  Get-Date -Format yyyyMMdd
PS>  Get-Date -UFormat %Y%m%d

May 31, 2016  3:19 PM

Counting members

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

If you have a collection of objects

$proc = get-process

you can get the number of members using the Length property

PS>  $proc.Length

$proc is of type System.Array

PS>  $proc.GetType()

IsPublic IsSerial Name                                     BaseType
——– ——– —-                                     ——–
True     True     Object[]                                 System.Array

MSDN documenattion is at https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx

PowerShell also adds a Count property that is an alias of Length

if you are after the number of members in a collection via the pipeline you need to use the Measure-Object cmdlet

PS>  $proc | Measure-Object
Count    : 71
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

or measure directly

PS>  Get-Process | Measure-Object
Count    : 71
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

Notice that Measure-Object can also be used to discover other statistics – the Average, Sum, Maximum and Minimum. These only work for numeric properties.

May 30, 2016  4:20 PM

Windows Server eras

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Windows Server 2016

This is worth a read


It discusses the evolution of Windows server and PowerShell. Read it in conjunction with the videos of Jeffrey Snover’s talks at WinOps and the European PowerShell conference this year.

May 30, 2016  10:02 AM

WMF 5.0 preview support ending

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Support for the WMF 5.0 preview is ending – https://blogs.msdn.microsoft.com/powershell/2016/05/23/end-of-support-for-windows-management-framework-wmf-5-0-production-preview-is-approaching-upgrade-to-rtm-bits/

Upgrade now!

May 29, 2016  3:58 AM

PowerShell 5.1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Want to see what PowerShell 5.1 looks like?

Install Windows Server 2016 TP 5 or get on the fast ring for Windows 10 Insider Previews. The latest build – 14352 shows a PowerShell version of  5.1.14352.1002 with Powershell 1.0 through 5.0 as compatible versions

The WSMAN stack is still version 3.0 so no issues with remoting or CIM sessions between PowerShell 3.0, 4.0, 5.0 and 5.1

PowerShell 2.0 will have the CIM session issue as it uses WSMAN stack 2.0 and PowerShell 1.0 didn’t have remoting

May 28, 2016  8:14 AM

Slides and Code from my WinOps 2016 talk

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The slides and code from my WinOps 2016 talk can be found here:


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: