PowerShell for Windows Admins


January 12, 2017  10:48 AM

Using $_

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The $_ symbol seems to be causing confusion from some recent forum questions I’ve seen.

$_ represents the current object on the pipeline – if you want to know why $_ was chosen you’ll have to read PowerShell in Action!

You can use $_ in a number of situations – in commands that perform an action on every object (or selected objects) on the pipeline. Here’s some of the commoner usages:

In the early versions of PowerShell you used it in the filter script of Where-Object

Get-Process | Where-Object -FilterScript {$_.CPU -gt 50}

This is more usually written as

Get-Process | Where {$_.CPU -gt 50}

As of PowerShell v3 if you are filtering on ONE property you can simplify the syntax

Get-Process | Where CPU -gt 50

which is a truncated version of

Get-Process | Where -Property CPU -gt -Value 50

When you write it like this its obvious what is happening. Get in the habit of thinking of the syntax in this manner even if you write in the shortened form

If you need to filter on TWO or more properties you have to use the old style syntax

Get-Process | Where {$_.CPU -gt 50 -AND $_.Handles -gt 1000}

In all of these cases you’re comparing a property of the current pipeline object against a value. If the comparison is true the object is passed onto the next step of the pipeline. If its false the object is discarded.

You can use $psitem in place of $_ if you prefer

Get-Process | Where {$psitem.CPU -gt 50}

In the Foreach-Object cmdlet you can use $_ to refer to the current object

Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true" |
ForEach-Object {
$ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($_.DeviceId)"
$props = @{
Name = $_.NetConnectionId
Product = $_.ProductName
DHCP = $ip.DHCPEnabled
IP   = $ip.IPAddress
}
New-Object -TypeName PSObject -Property $props
}

$psitem also works in this situation

The third common usage of $_ is in select-object in calculated fields (you can do the same in Format-Table)

Get-CimInstance -ClassName Win32_LOgicalDisk -Filter "DriveType=3" |
select DeviceId, VolumeName,
@{N='Size(GB)'; E={[math]::Round($_.Size / 1GB, 2)}},
@{N='Used(GB)'; E={[math]::Round(($_.Size - $_.FreeSpace) / 1GB, 2)}},
@{N='Free(%)'; E={[math]::Round(($_.FreeSpace / $_.Size) * 100, 2 )}}

In this example we’re changing the size to GB from bytes and calculating the used space and the % free space. Again you could use $psitem instead of $_

ONE PLACE YOU CAN’T USE $_  IS IN A FOREACH LOOP

This fails

$nics = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true"
$data = foreach ($nic in $nics) {
$ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($_.DeviceId)"
$props = @{
Name = $_.NetConnectionId
Product = $_.ProductName
DHCP = $ip.DHCPEnabled
IP   = $ip.IPAddress
}
New-Object -TypeName PSObject -Property $props
}
$data |Format-List

To recap $_ (or $psitem) is used to represent the current object on the pipeline. You can us it in commands that are performing an action on every object on the pipeline.

January 9, 2017  1:49 PM

Foreach confusion: Part two

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

My post on foreach seems to have generated a bit of confusion. I was trying to use the calculations as examples not a statement that you should do things that way – there were statements to that effect in the post but they seem to have been missed.

Any way, to try and resolve the confusion here’s another couple of examples. This time getting network adapters and finding the matching IP address.

First using Foreach-Object on a pipeline

Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true" |
ForEach-Object {
  $ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($psitem.DeviceId)"
  $props = @{
    Name = $psitem.NetConnectionId
    Product = $psitem.ProductName
    DHCP = $ip.DHCPEnabled
    IP   = $ip.IPAddress 
  }
  New-Object -TypeName PSObject -Property $props
} | Format-List

You can’t pipe Get-CimInstance into another call to the same cmdlet so you need to solve the problem using foreach.

Now the same problem solved using a foreach loop

$nics = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true"
$data = foreach ($nic in $nics) {
  $ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($nic.DeviceId)"
  $props = @{
    Name = $nic.NetConnectionId
    Product = $nic.ProductName
    DHCP = $ip.DHCPEnabled
    IP   = $ip.IPAddress 
  }
  New-Object -TypeName PSObject -Property $props
} 
$data |Format-List


January 6, 2017  3:55 PM

Is PowerShell just for administration?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Opinion, Powershell

Two related questions were left on my blog recently.

Is PowerShell just for administration?

Should I learn PowerShell, VBScript or cmd tools?

PowerShell was introduced as Microsoft’s automation engine for the Windows platform. It includes a scripting language, a shell and  the Integrated Scripting Environment (ISE). Other additions over the years have included Powershell workflows and Desired State Configuration.

The extension of PowerShell to Linux and Mac extends the administrative capabilities.

I would say that PowerShell  is primarily about the administration of Windows machines. This will be extended in time but at the moment its a Windows based administration tool.

Does that mean that its all you can do with it – Very definitely Not for example I recently wrote a script that would look at a large set of photos and move them into the correct folder based on the month they were created. Doing this by hand for a few photos is easy – for hundreds you need a script.

Over the years I’ve seen PowerShell code to do many non-administrator based things – for instance:

– manage and modify photos and videos

– play space invaders

– calculate the cooking time of a chicken given its weight

– work out the apparent temperature using air temperature, wind speed and altitude

– create web sites

PowerShell is .NET based which means you can work with just about the whole of the .NET framework in your PowerShell code. You can even write GUI applications in PowerShell if you want.

If you have a task that you perform on a Windows machine its very probable that a script could be written to perform that task. Creating the script may be very easy or very difficult but it should be possible.

If you want to do this sort of thing you need to:

– work out how to do the task manually

– break that down into a number of steps

– create the code to perform each step

– join it all together and test

If any one has suggestions for tasks they’d like to do leave a comment and I’ll see if I can suggest how you could do it

PowerShell or VBScript or cmd tools is fairly easy decision for me.  VBScript will receive no further development as far as I’m aware. The cmd tools are OK for specific jobs but don’t have the breadth that PowerShell does. CMD batch files are no where as caapble asa PowerShell script.

Learn PowerShell and use it as you want to make your computer use easier


January 3, 2017  11:47 AM

foreach confusion

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

One of the biggest sources of confusion to people learning PowerShell is foreach.

Don’t worry – it is confusing. Having just read the help file about_foreach its doubly confusing. That help file desperately needs a re-write

Foreach is used in two separate ways with two separate meanings

Firstly there is the foreach language statement or foreach loop. This is used to iterate over a collection of items

$disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" 
foreach ($disk in $disks){
  $fp = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($disk.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($disk.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

In this case the Win32_LogicalDisk CIM class is used to create a collection of disk information. The foreach language statement (loop) is used to work through that collection. For each disk in the disks collection we calculate the percentage free space. If that percentage is less than 60 a warnign message is displayed otherwise an information message is displayed – for versions of PowerShell before 5.0 use Write-Host instead of Write-Information.

PLEASE DON’T COMMENT THAT YOU WOULD DO THIS ANOTHER WAY – SO WOULD I. I’M USING IT TO ILLUSTRATE FOREACH!

Alternatively you could do this

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" |
foreach {
  $fp = [math]::Round(($psitem.FreeSpace / $psitem.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($psitem.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($psitem.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

Get the disk information but this time pass it into foreach on the pipeline

AGAIN PLEASE DON’T COMMENT THAT YOU WOULD DO THIS ANOTHER WAY – SO WOULD I. I’M USING IT TO ILLUSTRATE FOREACH!

This is where the confusion arises – foreach is used in two different ways.

In the second set of code – foreach is actually an alias for Foreach-object so the code could be written

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" |
ForEach-Object -Process {
  $fp = [math]::Round(($psitem.FreeSpace / $psitem.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($psitem.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($psitem.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

which makes more sense and is less confusing

Couple of tips:

Foreach in the pipeleine is an alias for foreach object and foreach on its own is the language statement

When writing code use Foreach-Object rather than foreach to reduce confusion


January 2, 2017  3:44 AM

New Year book offer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
book, Powershell

The New Year starts very nicely with POwerShell in Action, 3e being Mannings deal of the day for 3 January 2017

Half off my book Windows PowerShell in Action, Third Edition. Use code dotd010317au at http://bit.ly/2iw7MD2

Links should be directed to the DOTD’s page at https://www.manning.com/dotd


December 31, 2016  5:35 PM

PowerShell in 2017

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

As 2016 winds down what do we have to look forward to from PowerShell?

In January WMF 5.1 should become available for down level clients. This brings PowerShell 5.1 to Windows 7 SP1, 8.1, 2008 R2 SP1, 2012, 2012 R2

Notice that Windows 8 and 2008 aren’t on that list

If you’re using DSC I’d recommend bringing all your DSC servers and target machines up to 5.1

April brings the PowerShell Summit in Bellevue Washington USA and the PowerShell conference in Hannover Germany

Hopefully, Bruce Payette and I will get PowerShell in Action third edition  finished and published

PowerShell 6.0 will continue to be developed and the current alpha builds may turn into betas

Beyond that – who knows – watch this space…


December 31, 2016  1:36 PM

2016–a PowerShell perspective

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

2016 was important as the 10th anniversary of PowerShell being released as a web download – announced November 2006 at TechEd/IT Forum in Barcelona. The PowerShell team organised a 8 hour web cast on Channel 9 that I was privileged to be part of.

We also saw PowerShell 5.1 become available on Windows 10 and server 2016 (but no PowerShell for managing containers!!!)

April saw the PowerShell Summit which returned to Bellevue. The sell out event went very well with some great sessions and participation from the community and especially from the PowerShell team who went above and beyond. Already looking forward to next years event.

I also attended the PowerShell conference in Hannover, Germany and had a great time.

May saw the one day WinOps event in London – hope that becomes an annual event

The big – OK gigantic –event was the move of PowerShell core to the open source and the release of alpha versions of the code for Windows, Linux and Mac. You can mow remote between Windows and Linux!! How this develops is going to be interesting as it is a potentially breaking event for PowerShell or it could be the greatest thing ever only time will tell.

2016 was an interesting year and a fitting 10th anniversary for one of the best things to come out of Microsoft


December 30, 2016  2:17 PM

Summit 2017 Registration still open

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Just a quick reminder that the registration for the 2017 PowerShell and DevOps Summit is still open – details from https://powershell.org/summit/


December 30, 2016  2:10 PM

Preserving property order

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This is a very common pattern:

$os = Get-CimInstance -ClassName Win32_OperatingSystem 
$comp = Get-CimInstance -ClassName Win32_ComputerSystem

$props = @{
  OS = $os.Caption
  InstallDate = $os.InstallDate
  LastBoot = $os.LastBootUpTime
  Make = $comp.Manufacturer
  Model = $comp.Model
}

New-Object -TypeName PSObject -Property $props

Get some data – in this case a couple of WMI classes and create an output object.

Unfortunately, the output looks like this

Make        : Microsoft Corporation
Model       : Surface Pro 2
LastBoot    : 30/12/2016 09:41:52
OS          : Microsoft Windows 10 Pro Insider Preview
InstallDate : 08/12/2016 13:20:04

The property order is NOT preserved because you’re working with a hash table. If you absolutely have to preserve the property order use an ordered hash table

$os = Get-CimInstance -ClassName Win32_OperatingSystem 
$comp = Get-CimInstance -ClassName Win32_ComputerSystem

$props = [ordered]@{
  OS = $os.Caption
  InstallDate = $os.InstallDate
  LastBoot = $os.LastBootUpTime
  Make = $comp.Manufacturer
  Model = $comp.Model
}

New-Object -TypeName PSObject -Property $props

All you do is add [ordered] in front of the hashtable definition and your output becomes

OS          : Microsoft Windows 10 Pro Insider Preview
InstallDate : 08/12/2016 13:20:04
LastBoot    : 30/12/2016 09:41:52
Make        : Microsoft Corporation
Model       : Surface Pro 2

exactly what you defined


December 29, 2016  2:52 PM

Linux on the desktop

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Linux, Windows 10

My sense of humour has some quirky moments – as my friends will tell you. Last night I had a lot of time to sit and think (why is a story for another time and place) and it one point a thought about Linux on the desktop struck me.

Back in the 1980s when I worked in seismic exploration and desktop computers were emerging I remember seeing articles at least once every year that “this is the year that Unix takes over the desktop”

This then morphed to “this is the year that Linux takes over the desktop”

Of course neither of these fantasies were actually likely to happen – and I’m deliberately ignoring the Mac OS which is unix based.

However, my thought yesterday was that *nix on the desktop is actually a reality. Windows 10 includes the Bash on Ubuntu on Windows option which brings the bash shell to Windows.

Bet this way of getting *nix on the desktop was ever envisaged. Smile


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: