commands detail - d

date

The Powershell equivalent of the Unix date is

  1. get-date

The Powershell equivalent of the Unix date -s is

  1. set-date

I was anticipating doing a fairly tedious exercise of going through all the Unix date formats and then working out the Powershell equivalent, but discovered the Powershell Team has effectively done all this for me. There is a Powershell option -UFormat which stands for ‘unix format’.

So the Powershell:

  1. date -Uformat '%D'
  2. 09/08/14

is the same as the *nix

  1. date +'%D'
  2. 09/08/14

This is handy…but I have found the odd difference. I tried this for a demo:

Unix:

  1. date +'Today is %A the %d of %B, the %V week of the year %Y. My timezone is %Z, and here it is %R'
  2. Today is Monday the 08 of September, the 37 week of the year 2014. My timezone is BST, and here it is 17:24

Powershell:

  1. get-date -Uformat 'Today is %A the %d of %B, the %V week of the year %Y. My timezone is %Z, and here it is %R'
  2. Today is Monday the 08 of September, the 36 week of the year 2014. My timezone is +01, and here it is 17:25

I presume the discrepancy in the week of the year is to do with when the week turns - as you can see I ran the command on a Monday. Some systems have the turn of the week being Monday, others have it on Sunday.

I don’t know why %Z outputs different things….and I can’t help feeling I’m being churlish pointing this out. The -UFormat option is a really nice thing to have.

df -k

A quick and dirty Powershell equivalent to ‘df -k’ is

  1. Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" | ft

A slightly prettier version is this function:

  1. function get-serversize { Param( [String] $ComputerName)
  2. Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer $ComputerName |
  3. Select SystemName, DeviceID, VolumeName,
  4. @{Name="size (GB)";Expression={"{0:N1}" -f($_.size/1gb)}},
  5. @{Name="freespace (GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}
  6. }
  7. function ss { Param( [String] $ComputerName)
  8. get-serversize $ComputerName | ft
  9. }

….then you can just do:

  1. $ ss my_server

….and get

  1. SystemName DeviceID VolumeName size(GB) freespace(GB)
  2. ---------- -------- ---------- -------- -------------
  3. my_server C: OS 30.0 7.8
  4. my_server D: App 250.0 9.3
  5. my_server E: 40.0 5.0

dirname

A good PowerShell equivalent to the unix dirname is

  1. gi c:\double_winners\chelsea.doc | select directory

However, this isn’t a direct equivalent. Here, I’m telling Powershell to look at an actual file and then return that file’s directory. The file has to exist. The unix ‘dirname’ doesn’t care whether the file you specify exists or not. If you type in dirname /tmp/double_winners/chelsea.doc on any Unix server it will return /tmp/double_winners, I think. dirname is essentially a string-manipulation command.

A more precise Powershell equivalent to the unix ‘dirname’ is this

  1. [System.IO.Path]::GetDirectoryName('c:\double_winners\chelsea.doc')

….but it’s not as easy to type, and 9 times out of 10 I do want to get the folder for an existing file rather than an imaginary one.

du

While I think there are implementations of du in PowerShell, personally my recommendation would be to download Mark Russinovich’s ‘du’ tool, which is here:

Windows Sysinternals - Disk Usage

This is part of the Microsoft’s ‘sysinternals’ suite.