PowerShell for Windows Admins


June 13, 2011  3:06 PM

Calculated Fields

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You have probably seen something like this many times

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
select @{Name="FreeSpace";Expression={$_.Freespace / 1GB}}

We take an object and perform a calculation to in effect make a new property – a calculated field. Label can be used instead of Name if desired in PowerShell 2

This can also be done in Format-Table

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}}

There some other parameters we can use

We can control the alignment in the field

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Alignment="Left"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Alignment="Center"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Alignment="Right"}

We can format the string

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Format="F2"; Alignment="Left"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Format="F2"; Alignment="Center"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Format="F2"; Alignment="Right"}

And we can control the width of the string

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Width=11; Format="F2"; Alignment="Left"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Width=11; Format="F2"; Alignment="Center"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Width=11; Format="F2"; Alignment="Right"}

 

Width, format and alignment don’t work with select-object

June 12, 2011  1:45 PM

PowerShell User Group–21 June 2011

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


When: Tuesday, Jun 21, 2011 7:30 PM (BST)


Where: Virtual

*~*~*~*~*~*~*~*~*~*

Session will look at using PowerShell to automate Microsoft Office – Word, Excel, Visio, Access and more

Notes


Richard Siddaway has invited you to attend an online meeting using Live Meeting.
Join the meeting.
Audio Information
Computer Audio
To use computer audio, you need speakers and microphone, or a headset.
First Time Users:
To save time before the meeting, check your system to make sure it is ready to use Microsoft Office Live Meeting.
Troubleshooting
Unable to join the meeting? Follow these steps:

  1. Copy this address and paste it into your web browser:
    https://www.livemeeting.com/cc/usergroups/join
  2. Copy and paste the required information:
    Meeting ID: Z7FFBT
    Entry Code: TC%f8)D(2
    Location: https://www.livemeeting.com/cc/usergroups

If you still cannot enter the meeting, contact support

Notice
Microsoft Office Live Meeting can be used to record meetings. By participating in this meeting, you agree that your communications may be monitored or recorded at any time during the meeting.


June 9, 2011  3:14 PM

Date Formats–custom formats

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve added examples of practically all of the custom elements that can be used in a date format

"d ShortDatePattern                                                :  {0} " -f (get-date -Format d )
"D LongDatePattern                                                 :  {0} " -f (get-date -Format D )
"f Full date and time (long date and short time)                   :  {0} " -f (get-date -Format f )
"F FullDateTimePattern (long date and long time)                   :  {0} " -f (get-date -Format F )
"g General (short date and short time)                             :  {0} " -f (get-date -Format g )
"G General (short date and long time)                              :  {0} " -f (get-date -Format G )
"m MonthDayPattern                                                 :  {0} " -f (get-date -Format m )
"M MonthDayPattern                                                 :  {0} " -f (get-date -Format M )
"o Round-trip date/time pattern always uses the invariant culture  :  {0} " -f (get-date -Format o )
"O Round-trip date/time pattern always uses the invariant culture  :  {0} " -f (get-date -Format O )
"r RFC1123Pattern always uses the invariant culture                :  {0} " -f (get-date -Format r )
"R RFC1123Pattern always uses the invariant culture                :  {0} " -f (get-date -Format R )
"s SortableDateTimePattern always uses the invariant culture       :  {0} " -f (get-date -Format s )
"t ShortTimePattern                                                :  {0} " -f (get-date -Format t )
"T LongTimePattern                                                 :  {0} " -f (get-date -Format T )
"u UniversalSortableDateTimePattern                                :  {0} " -f (get-date -Format u )
"U Full date and time – universal time                             :  {0} " -f (get-date -Format u )
"y YearMonthPattern                                                :  {0} " -f (get-date -Format y )
"Y YearMonthPattern                                                :  {0} " -f (get-date -Format Y )

"`nCustom Formats"
"d/M/y                                  :  {0} " -f (get-date -Format d/M/y )
"%d/%M/yy                               :  {0} " -f (get-date -Format d/M/yy )
"dd/MM/yyyy                             :  {0} " -f (get-date -Format dd/MM/yyyyy )
"dd/MM/yyyy %g                          :  {0} " -f (get-date -Format ‘dd/MM/yyyyy %g’)
"dd/MM/yyyy gg                          :  {0} " -f (get-date -Format ‘dd/MM/yyyyy gg’)
"dddd dd/MM/yyyy gg                     :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy gg’)
"dddd dd/MM/yyyy %h:m:s tt gg           :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy %h:m:s tt gg’)
"dddd dd/MM/yyyy hh:mm:s tt gg          :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy hh:mm:s tt gg’)
"dddd dd/MM/yyyy HH:mm:s gg             :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy HH:mm:s gg’)
"dddd dd/MM/yyyy HH:mm:s.ffff gg        :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy HH:mm:s.ffff gg’)
"dddd dd MMM yyyy HH:mm:s.ffff gg       :  {0} " -f (get-date -Format ‘dddd dd MMM yyyyy HH:mm:s.ffff gg’)
"dddd dd MMMM yyyy HH:mm:s.ffff gg      :  {0} " -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff gg’)
"dddd dd MMMM yyyy HH:mm:s.ffff zz gg   :  {0} " -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff zz gg’)
"dddd dd MMMM yyyy HH:mm:s.ffff zzz gg  :  {0} " -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff zzz gg’)

The various custom elements can be combined in numerous other ways but these examples should show you how they all work.


June 8, 2011  1:34 PM

PowerShell and WMI forum

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I will be moderating a new forum for PowerShell on powershell.com  http://powershell.com/cs/forums/217.aspx

The forum is slanted towards PowerShell and WMI but happy to take questions on any aspect of PowerShell


June 6, 2011  1:57 PM

Get-WmiObject property parameter

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This is a fairly obvious piece of PowerShell

Get-WmiObject Win32_Volume

We would probably restrict the properties we displayed like this

Get-WmiObject Win32_Volume | select DriveLetter, DriveType, Freespace

or possibly

Get-WmiObject Win32_Volume |
format-table DriveLetter, DriveType, Freespace –AutoSize

If we are dealing with a large number of remote machines we may want to restrict the properties we return – this is where we use the –property parameter

Get-WmiObject Win32_Volume -property @("DriveLetter", "DriveType", "Freespace")

NOTE THIS STILL RETURNS THE SYSTEM PROPERTIES

so to get a good display we would want to still use format-table (or select)

Get-WmiObject Win32_Volume -property @("DriveLetter", "DriveType", "Freespace") |
format-table DriveLetter, DriveType, Freespace –AutoSize

The –property parameter gives us another opportunity to restrict the data returned to reduce network traffic but we still need to format the returned data


June 3, 2011  12:54 PM

Invoke-WmiMethod parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The Invoke-WmiMethod cmdlet was new with PowerShell 2. 

In PowerShell v1 we would do something like this

(gwmi win32_Process -Filter "Name=’Notepad.exe’").Terminate()

With the cmdlet we can do this

gwmi win32_Process -Filter "Name=’Notepad.exe’" |
Invoke-WmiMethod -Name Terminate

This works OK when the method doesn’t need any arguments. When arguments are necessary you may find that using the method directly on the object works BUT it doesn’t if you use Invike-WmiMethod with the –Arguments  parameter.

One possibility is that the cmdlet needs the parameters in a different order.

If you test the arguments needed by a method like this

(gwmi win32_volume -Filter "DriveLetter = ‘c:’" ).Chkdsk.OverLoadDefinitions

You will get the order shown in the documentation

if you use

(gwmi win32_volume -Filter "DriveLetter = ‘c:’" ).GetMethodParameters("Chkdsk")

You may find you get different results for the order of the parameters. Use the order given by this technique with the Invoke-WmiMethod cmdlet


June 3, 2011  5:13 AM

Powers

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Many numbers in computing are based on powers of 2. I need to calculate some powers of 2 and realised that PowerShell doesn’t have  an operator for raising  a number  to a power.  In many languages ** or ^ supply this functionality.

In PowerShell we drop back to .NET and use the System.Math class (still think that should be Maths!)

[math]::Pow(2,2) raises 2 to the power 2 and gives us the answer of four.

1..30 | foreach {[math]::Pow(2,$_)}

prints out the first 30 powers of two

Just out of interest 1GB is 2 to the power 30

We can make this a bit prettier

1..30 | foreach {
"{0,3} {1,15}" -f $($_), $([math]::Pow(2,$_))
}

Check the values for 1kb, 1mb and 1gb which are

1024
1048576
1073741824

respectively.

I’ll convert this to a function and add it to the PAM maths module.


May 30, 2011  7:36 AM

PowerShell UG–June 2011 reminder

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Don’t forget to add the June meeting of the UK PowerShell User group to your calendar.  Its June 21 at 7.30pm BST. Details available from

http://msmvps.com/blogs/richardsiddaway/archive/2011/05/10/powershell-uk-user-group-june-meeting.aspx


May 29, 2011  2:57 AM

Manning Deal of the Day–29 May

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Today, for one day only, get 50% off PowerShell and WMI MEAP or MEAP+ebook.

 

The code is dotd0529cc when you order from www.manning.com

 

The same code can be used for PowerShell in Action – second edition and PowerShell in Practice


May 27, 2011  12:17 PM

root\wmi–MSBatteryClass

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A number of the classes in root\wmi return results from more than one class. That sounds odd but it can be explained by an example.

The namespace contains a number of classes related to the battery in laptops

gwmi -Namespace root\wmi -List *battery*

MSBatteryClassEvent
BatteryStatusChange
BatteryTagChange
MSBatteryClass
BatteryStaticData
BatteryRuntime
BatteryCycleCount
BatteryTemperature
BatteryStatus
BatteryFullChargedCapacity

 

We’ll ignore the event and change classes for now.  If we pick out the MSBattery class we get information from a number of other classes returned – MSBattery is a super class.

PS> gwmi -Namespace root\wmi -Class MSBatteryClass | select __class

__CLASS
——-
BatteryCycleCount
BatteryFullChargedCapacity
BatteryStaticData
BatteryRuntime
BatteryStatus

 

Be aware that the runtime property returned by BatteryRuntime doesn’t respond to calculations you may find on the Internet when your OS is Windows 7

Battery status is a useful class to determine if you are on battery or external power & if the battery is charging


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: