PowerShell for Windows Admins


February 19, 2014  12:15 PM

Oh–so that’s what its for



Posted by: Richard Siddaway
PowerShell Basics

I’ve never really understood the logic behind the Wait-Job cmdlet. The idea of PowerShell Jobs is to kick of tasks that grind away in the background and you get the prompt back so you can keep on working. Wait-Job stops the prompt being returned until one or more jobs have finished.

It clicked this morning – I need to run some tasks in parallel and decided that a number of jobs was the best way (I looked at workflow but there are some complications such that I didn’t pursue that route). The only complication with using Jobs is that you don’t know when they finish (unless you check with Get-Job) and I needed a number of Jobs to finish before starting the last job in the series – this is a data dependency issue that I can’t avoid.

Wait-Job is the answer. Start my first set of jobs. Use:

Get-Jobs | Wait-Job

which means nothing else is done until all my jobs finish and then start the last job.

As I’m going to be running this through a scheduled task I don’t care about the prompt being frozen.

Nice to finally find a use for a cmdlet I’ve ignored since it appeared.

February 19, 2014  12:02 PM

Windows PowerShell Networking Guide



Posted by: Richard Siddaway
Books, Network, PowerShell

A free ebook on managing various aspects of your networking is now available from http://powershell.org/wp/2014/02/19/free-ebook-from-microsofts-scripting-guy-windows-powershell-networking-guide/

The book is written by Ed Wilson – The Scripting Guy – and is a must read for any PowerShell user.


February 18, 2014  2:29 PM

The next Scripting Games



Posted by: Richard Siddaway
Scripting Games

No – we’re not announcing the start of the next games just yet.

There is however a chance for you to shape the next games – head over to http://powershell.org/wp/2014/02/17/what-should-the-scripting-games-look-like-next-time/ and tell us what you would like to see in the next games


February 17, 2014  4:11 PM

Winter 2014 Scripting Games closed



Posted by: Richard Siddaway
Scripting Games 2014

The Winter 2014 Scripting Games are officially closed

http://powershell.org/wp/2014/02/17/closing-the-games/

And the winners declared.

A big thank you to all who took part as competitors, coaches or judges.

We will be back with a Summer games later in the year – stay tuned for further details


February 17, 2014  2:34 AM

PowerShell Hello World



Posted by: Richard Siddaway
PowerShell Basics

I had a comment left on one of my posts recently that mentioned a “Hello World” script. The concept of a Hello World program/script is that it is a simple introduction to using a new language that gives a known result.

For a brand new user to PowerShell is is how you create a Hello World script:

open PowerShell with elevated privileges (Run as Administrator)

type  Set-ExecutionPolicy RemoteSigned –Force and press enter

This enables scripts to run.

Run these two lines by typing them individually and pressing enter

New-Item -Path c:\ -Name TestScripts -ItemType Directory cd C:\TestScripts

They create a test folder and move your location to that folder

Type

“‘Hello World’” > script1.ps1

and press enter.  This creates your script. The single quotes nested in the double quotes are so that the text in the script will be surrounded by quotes – its a string value.

Run the script by typing

.\script1.ps1

and pressing enter.  You will see the results:

Hello World

The .\ is required as PowerShell won’t run commands in the current folder by default. You have to explicitly give the local path.

You’ve now written your first PowerShell script and discovered most of the “gotchas” that cause people problems when they are first learning PowerShell


February 13, 2014  1:43 PM

Learn Windows IIS in a Month of Lunches



Posted by: Richard Siddaway
Books, IIS, PowerShell

Jason Helmick’s book in the Month of Lunches series is now available – http://www.manning.com/helmick/

I really can’t recommend this book enough – if you’re new to IIS or want to learn how to administer IIS with PowerShell – this is the book for you.

Buy it – you won’t be disappointed.


February 12, 2014  1:16 PM

PowerShell Summit–Europe 2014



Posted by: Richard Siddaway
PowerShell, Summit

The dates of the European PowerShell for 2014 have been announced – 29 September 29 to 1 October at the Hotel park in Amsterdam – http://powershell.org/wp/community-events/summit/powershell-summit-europe/

We are starting to put together an agenda that will feature speakers from Europe, US and the PowerShell team.

Registration details will be announced in the not too distant future – but save the dates.  You won’t want to miss this.


January 22, 2014  1:56 PM

When did Windows update last run



Posted by: Richard Siddaway
PowerShell, Registry

A question came up on the forum regarding when Windows Update last run and when an update was last installed.  Get-Hotfix shows the date of installation for most BUT not all patches.

The registry holds values showing last successful detection and install:

$props = [ordered]@{ LastDetect = Get-ItemProperty -Path ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Detect’ -Name LastSuccessTime | select -ExpandProperty LastSuccessTime

LastInstall = Get-ItemProperty -Path ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install’ -Name LastSuccessTime | select -ExpandProperty LastSuccessTime }

New-Object -TypeName psobject -Property $props


January 22, 2014  12:30 PM

Win32_OperatingSystem examples



Posted by: Richard Siddaway
CIM, PowerShell 3, PowerShell v4

The Win32_ComputerOperatingSystem class can provide a good deal of information about the OS installed on your machines. These examples are converted from those presented here: http://msdn.microsoft.com/en-us/library/aa394596%28v=vs.85%29.aspx

# ServicePack version

Get-CimInstance -ClassName Win32_OperatingSystem |

select ServicePackMajorVersion, ServicePackMinorVersion

# install date of OS

Get-CimInstance -ClassName Win32_OperatingSystem |

select Installdate

# Windows version

Get-CimInstance -ClassName Win32_OperatingSystem |

select Caption, Version

# windows folder

Get-CimInstance -ClassName Win32_OperatingSystem |

select WindowsDirectory

# all

Get-CimInstance -ClassName Win32_OperatingSystem |

select Caption, Version, ServicePackMajorVersion,

ServicePackMinorVersion, Installdate, WindowsDirectory

You could create a function:

function get-OS {

[CmdletBinding()]

param(

[string]$computername = $env:COMPUTERNAME

)

Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computername|

select Caption, Version, ServicePackMajorVersion,

ServicePackMinorVersion, Installdate, WindowsDirectory

}

and then choose properties if required:

£> get-OS | Format-Table Caption, Installdate

Caption                                       Installdate

——-                                           ———–

Microsoft Windows 8.1 Pro    05/12/2013 10:16:49

£> get-OS

Caption : Microsoft Windows 8.1 Pro

Version : 6.3.9600

ServicePackMajorVersion : 0

ServicePackMinorVersion : 0

Installdate : 05/12/2013 10:16:49

WindowsDirectory : C:\windows

£> get-OS | Format-Table Caption, Service* -AutoSize

Caption                                    ServicePackMajorVersion   ServicePackMinorVersion

——-                                         ———————–             ———————–

Microsoft Windows 8.1 Pro   0                                              0


January 20, 2014  1:30 PM

Win32_Process examples–running applications



Posted by: Richard Siddaway
CIM, PowerShell 3, PowerShell v4

You can see the running processes on a local or remote machine using Get-Process. Alternatively you can use Win32_Process:

Get-CimInstance -ClassName Win32_Process | select Name, ProcessID, Threadcount, PageFileUsage, PageFaults, WorkingSetSize | Format-Table –AutoSize

You can use the –ComputerName or –CimSession properties to access the processes on a remote machine.

Other properties are available:

Get-CimClass -ClassName Win32_Process | select -ExpandProperty CimClassProperties | Format-Table -AutoSize


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: