Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Weekend Scripter: Use PowerShell to Count Pictures on a Picture Volume

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to count pictures and to display free space on his picture volume.

Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about visiting places is the opportunity to take pictures—at least that is a major attraction for me. Don’t get me wrong. Charlotte, North Carolina, is a pretty city, and there are lots of nice places to take pictures, but dude, nothing in Charlotte compares with the castle in Prague that we saw while the Scripting Wife and I were visiting Windows PowerShell MVP David Moravec and his lovely wife, Andrea. I mean the NASCAR museum is nice (and it is not even really in Charlotte), but it’s not quite as cool as a 1,200-year-old castle seen here.

1,200-year-old castle

Counting picture files

So, anyway, I went crazy and took over 800 pictures while we were in Prague. But how many pictures did I take during the entire trip? Well, here I can easily use Windows PowerShell to perform the count. My camera takes two types of pictures, RAW CR2 types of files and JPG files. I took a bit of both. The following is a Windows PowerShell 3.0 command (use of the –file switch).

PS C:\> Get-ChildItem f: -Recurse -Include *.cr2,*.jpg -File | measure

Count    : 9417

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

To do this on Windows PowerShell 2.0, I would use the following command.

PS C:\> Get-ChildItem f: -Recurse -Include *.cr2,*.jpg | where {!($_.ispscontainer) } | measure

Count    : 9417

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

If I only want to know how many CR2 files I took, I use the command shown here.

PS C:\> Get-ChildItem f: -Recurse -filter *.cr2 -File | measure

Count    : 4706

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

Here is the Windows PowerShell 2.0 version of the command.

PS C:\> Get-ChildItem f: -Recurse -filter *.cr2 | where {!($_.ispscontainer) } | measure

Count    : 4706

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

I can do the same thing for the JPG files, as shown here.

PS C:\> Get-ChildItem f: -Recurse -filter *.jpg -File | measure

Count    : 4711

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

Finding disk space info

Because I took nearly 10,000 pictures, I am concerned about disk space. On my laptop running Windows 8, I can use the following command to quickly obtain disk space information, as shown here.

Get-Volume -DriveLetter f

The command and the output associated with the command is shown here.

Image of command output

To use Windows PowerShell 2.0 (or even Windows PowerShell 3.0 on a computer not running Windows 8), you need to query the Win32_Volume WMI class (this class appears on Windows Server 2003 and later—not on Windows XP). The following command illustrates this technique, where GWMI is an alias for the Get-WmiObject cmdlet, and FT is an alias for the Format-Table cmdlet. Because WMI returns disk-sizing information in bytes, it is better to calculate the output and to convert it into at least gigabytes for easier reading. This is what the custom label commands do in the Format-Table cmdlet.

gwmi win32_volume -Filter "driveletter = 'f:'" | ft driveletter, label, @{LAB

EL='Size GB';EXPRESSION = {$_.capacity/1GB}}, @{LABEL='FREE GB';EXPRESSION = {$_.FreeSpace/1GB}} –AutoSize

The command and its associated output are shown here.

Image of command output

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 3333

Trending Articles