Introduction to PowerShell for Unix People by Powershell.org - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

7. commands detail - d

date

The Powershell equivalent of the Unix date is

img95.png

The Powershell equivalent of the Unix date -s is

img96.png

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:

img97.png

is the same as the *nix

img98.png

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

Unix:

img99.png

Powershell:

img100.png

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

img101.png

A slightly prettier version is this function:

img102.png

….then you can just do:

img103.png

….and get

img104.png

dirname

A good PowerShell equivalent to the unix dirname is

img105.png

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

img106.png

….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.