Showing posts from January, 2013

WPF / MVVM Resource Collection

PowerShell: Files and Directories

Many times we feel the need to get the list of files in a directory with all the file details. A picture (screenshot) is worth a thousand words (pixels) but at times it is not the best thing.

(1) Get the file list

Command: cd “C:\Program Files\Adobe\Reader 10.0\Reader”
Command: DIR

This will give us the list, nothing unusual.

(2) Send the list to a text file

Command: DIR > C:\FileList.txt

The file “FileList.txt” will contain the same text that was shown on console

(3) Get only the files or only directory list

The list contains both the directories and files, how do we get only the files? (we can’t do this in a explorer)

Command: PS C:\Program Files\Adobe\Reader 10.0\Reader> dir | where{$_.Attributes -ne "Directory"}

The output of DIR is sent to where{}, the “$_” is the current item, Attributes is a property, “-ne” means “not equal to”. Each item in the output of DIR is checked for having the “Directory” attribute. Fantastic!

To get only the directories – we change “-ne” to ‘-eq”

Windows PowerShell - Get new GUID

Recently I took a brief tour of PowerShell, it’s the Microsoft’s task automation framework. It would suit you more if you consider your keyboard as your friend :). At the moment, I do not know whether I think it is useful or not. I think the more I get to know the various commands, the more I will use and appreciate it.

I am writing this post, so that I can record the PowerShell commands that I begin to use. Here’s is the first one:

Get a new GUID: Currently I use Visual Studio 2005 to get a new GUID value. Now opening up VS just to do that is really an overkill.
In PowerShell we can get it by:

[guid] is a type adapter (you can say alias) to the full class name – System.Guid. NewGuid() is a static method on that class.

If we don’t remember the adapter but if we do know the full class name, we can use –> $guid = [System.Guid]::NewGuid()