February 28, 2020
Hot Topics:

An Introduction To PowerShell

  • By Josh Fitzgerald
  • Send Email »
  • More Articles »

DIR.PS1: The Header

A PowerShell script consists of PowerShell commands in a plain text file with a .PS1 extension. For your DIR replacement, you will use a text file called DIR.PS1.

To run the script, type the following at a PowerShell prompt:

.DIR.PS1 X:Folder

where X is a drive letter and Folder is the name of a folder.

The first part of your script will display the same header information that the CMD.EXE DIR command produces. You need to determine the drive letter of the path that was passed into the script. Arguments to scripts are stored in an array called $args (all PowerShell variables begin with a $). To keep things simple for now, you are only going to deal with the first argument that was passed into your script and ignore the rest. Therefore, you will create a variable called $drive and set it equal to $args[0] (0 is the first element of all arrays in PowerShell).

$drive = $args[0].SubString(0, 1).ToUpper()

To get some of the information you need about the drive, you will have to utilize Windows Management Instrumentation (WMI). The details of WMI is outside of the scope of this article, but the following PowerShell code is pretty easy to understand. You create a variable, $filter, to use with the Get-WmiObject command. The filter you have created tells the command that you only want information about a particular disk. The results of the Get-WmiObject command are stored in a variable called $volInfo. Remember, in PowerShell everything is an object; $volInfo is now an object returned from Get-WmiObject.

$filter = "DeviceID = '" + $drive + ":'"
$volInfo = Get-WmiObject -Class Win32_LogicalDisk -Filter $filter

You now have access to all of the properties and methods associated with this object. The serial number of the drive can be accessed via the VolumeSerialNumber property. The serial number is returned as an 8-character string, but you want to format it as the first 4 characters followed by a dash, and then the last 4 characters. In the line below, the backtick character at the end of the first line is the PowerShell line continuation character. Basically, it just tells PowerShell that the line continues on the next line. Splitting the line is not required, but I did it here to decrease the width of the line and to improve the readability of the script.

$serial = $volInfo.VolumeSerialNumber.SubString(0, 4) + "-" + `
          $volInfo.VolumeSerialNumber.SubString(4, 4)

Now that you have the $volInfo object, you can write the DIR header information to the screen. If a volume has no label, the text written to the screen is slightly different than if the volume does have a label. A simple If-Else statement is used to determine whether the VolumeName property is equal to an empty string. The Write-Host command is used to write each line to the screen.

If ($volInfo.VolumeName -eq "")
   { Write-Host (" Volume in drive " + $drive + " has no label") }
   { Write-Host (" Volume in drive " + $drive + " is " +
                 $volInfo.VolumeName) }
Write-Host (" Volume Serial Number is " + $serial)
Write-Host ("`n Directory of " + $args[0] + "`n")

The "`n" at the beginning and end of the last Write-Host command is used to insert a newline before and after the text. The Write-Host command adds a newline at the end of each line itself, so the effect of the "`n" is to make a blank line before and after that line of text.

Did you notice the "-eq" in the If statement? That is the equality comparison operator. The table below shows all of the comparison operators:

-eq, -ieq Equals
-ne, -ine Not equals
-gt, -igt Greater than
-ge, -ige Greater than or equal
-lt, -ilt Less than
-le, -ile Less than or equal

The -i versions of the comparison operators are the case-insensitive versions.

Figure 3 shows the output of the script you have so far.

Click here for a larger image.

Page 2 of 3

This article was originally published on April 30, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date