PowerShell Manipulating Objects


This entry is part 9 of 9 in the series PowerShell

To learn about objects in PowerShell we can have a look at the video called Manipulating Objects in Microsoft PowerShell – Video 2 that’s by Shane Young. In the video Shane builds the script up from simple to complex. The idea here is to simply show the disk drives on a computer, with the free space, used space, and total space. This post is based on that video.

PowerShell Tip: You can use the up and down arrows to scroll up and down through all of the commands you typed at the console. Another thing you can do is run get-history to display the list of commands you ran during your session.

If you run the command Get-PSDrive you get your disk drives as well as a few other items. We need to filter out those other items. We do this by only taking those drives with free space greater than one. The $_ represents the current object in the pipe. We piped (represented by the | character) each one over. The code above loops through all of the lines in the object one at a time. -gt represents greater than. Don’t forget to run start-transcript first.

get-psdrive | where-object {$_.free -gt 1}

When you run the command you get output similar to the following output, except that your drives are listed along with the used, free and root information.

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
C                 100.54         17.41 FileSystem    C:\
D                 610.13        302.37 FileSystem    D:\
F                1574.25        288.74 FileSystem    F:\

Let’s build this up to get output more to our liking. Next, we’ll look at Select-Object. We will simply type select instead of select-object because it’s easier to type. What columns do we need to display?

get-psdrive | where-object {$_.free -gt 1} | select Root, Used, Free

Root          Used         Free
----          ----         ----
C:\   108185092096  18456670208
D:\   638078373888 341704744960
F:\  1616512700416 383850487808

Select *

To learn a bit about what’s in each object go ahead and try the code below. Replace Root, Used, Free with the asterisk (*).

get-psdrive | where-object {$_.free -gt 1} | select *

Next, we’ll demonstrate the foreach-object. There are three lines in the output after we filtered the results of get-psdrive for only those that have a free value greater than one.

PS D:\MyData\Portfolio\PowerShellScripts> get-psdrive | where-object {$_.free -gt 1} | foreach-object {"some text"}
some text
some text
some text
PS D:\MyData\Portfolio\PowerShellScripts>

Instead of just outputtin some text we can output text and the root and free.

PS D:\MyData\Portfolio\PowerShellScripts> get-psdrive | where-object {$_.free -gt 1}| foreach-object { write-host "Free space for" $_.root "is" ("{0:N2}" -f ($_.Free/1gb)) -ForegroundColor red}
Free space for C:\ is 17.19
Free space for D:\ is 318.24
Free space for F:\ is 357.49
PS D:\MyData\Portfolio\PowerShellScripts>

Now let’s look to .NET for a formatting function. Below are some examples.

PS D:\MyData\Portfolio\PowerShellScripts> "{0:N0}" -f 1000000000000
1,000,000,000,000
PS D:\MyData\Portfolio\PowerShellScripts> "{0:N2}" -f 1000000000000
1,000,000,000,000.00
PS D:\MyData\Portfolio\PowerShellScripts> "{0:C2}" -f 1000000000000
$1,000,000,000,000.00
PS D:\MyData\Portfolio\PowerShellScripts> "{0:P2}" -f 0.9287451
92.87%
PS D:\MyData\Portfolio\PowerShellScripts> "{0:P4}" -f 0.9287451
92.8745%
PS D:\MyData\Portfolio\PowerShellScripts> "{0:P1}" -f 0.9287451
92.9%

Let’s use our new formatting.

PS D:\MyData\Portfolio\PowerShellScripts> get-psdrive | where-object {$_.free -gt 1}| foreach-object { write-host "Free space for" $_.root "is" ("{0:N2}" -f ($_.Free/1gb)) "GB" -ForegroundColor red}
Free space for C:\ is 17.20 GB
Free space for D:\ is 318.24 GB
Free space for F:\ is 357.49 GB

Let’s do some work on the ForEach-Object. It can take one set of commands as we had in the example above. Have a look at the brackets { } after the foreach-bject command in the example above. Two sets of brackets gives you a header and detail lines. Three sets of brackets gives you a header, detail lines and a footer line.

PS D:\MyData\Portfolio\PowerShellScripts> get-psdrive | where-object {$_.free -gt 1}| foreach-object { "one time only" }{"3 times"}
one time only
3 times
3 times
3 times
PS D:\MyData\Portfolio\PowerShellScripts> get-psdrive | where-object {$_.free -gt 1}| foreach-object { "one time only" }{"3 times"}{"one time at the end"}
one time only
3 times
3 times
3 times
one time at the end
PS D:\MyData\Portfolio\PowerShellScripts>

Below is Shane’s final script and the output on my computer. The F drive is an external USB drive. The ForEach-Object in the example below uses three sets of brackets. The first simply sets a variable to 0 and displays a blank line. Notice that the first set of brackets has two statements that are separated by the semi-colon. We need that semi-colon to end the statement, just as we use it the C# language.

PS D:\MyData\Portfolio\PowerShellScripts> get-psdrive | where-object {$_.free -gt 1}| foreach-object {$CountVariable = 0; Write-Host "";}{ $_.Name + ": Used: " + "{0:N2}" -f ($_.Used/1gb) + " Free: " + "{0:N2}" -f ($_.Free/1gb) + "GB" + " Total: " + "{0:N2}" -f (($_.Used/1gb)+($_.Free/1gb)); $CountVariable = $CountVariable + $_.Free;}{Write-Host ""; Write-Host "Total Free space " ("{0:N2}" -f ($CountVariable/1gb)) "GB" -BackgroundColor magenta}

C: Used: 100.75 Free: 17.20GB Total: 117.94
D: Used: 594.26 Free: 318.24GB Total: 912.49
F: Used: 1,505.49 Free: 357.49GB Total: 1,862.98

Total Free space  692.92 GB
PS D:\MyData\Portfolio\PowerShellScripts>

Instead you can use Get-Volume. Yes, that’s right, you could have used get-volume instead of writing this long command, but the point was to learn something about scripting.

Series Navigation<< PowerShell Objects