Get-ChildItem -ne dir

For the past little while I’ve had many opportunities to be able to speak in front of a lot of IT professionals (both administrators and developers) about PowerShell.  This has included touring with the EnergizeIT Certification Bootcamp User Group Tour and presenting at various local user group events here in Ottawa.  It also included helping out a little at the PowerShell demo station at the IT Pro week of TechEd 2008.  All of this has been great fun because I really love being able to talk face-to-face with IT professionals and help them with their problems (I guess sitting in a cubicle for 10 years writing code must have gotten to me).

During these presentations, one (of several) common messages that I have been delivering is this:

If you go into cmd.exe to run some command, do it in PowerShell instead.

In general, this recommendation works well because you can draw from skills you’ve acquired while using cmd.exe and apply those directly in PowerShell.  These commands don’t work exactly the same way they did outside of PowerShell, but in most cases you won’t notice and you can start getting comfortable using the PowerShell console instead of cmd.exe.

What about the cases where you will notice that one of the commands that you know and use doesn’t function the same way in PowerShell?  Let’s look at an example of a very common cmd.exe command that either doesn’t work in PowerShell in many cases without modification or that doesn’t give you the results you were looking for in others.  The command I’m talking about is the dir command.  Dir is a good example to use to illustrate this for several reasons:

  1. It is often the first command that anyone will try in PowerShell when they open it for the first time if they haven’t seen demonstrations showing all of the cool commands like Get-Service, Get-Process, Get-QADUser, etc. (aside from help, but what else are you going to try in a command shell you’re not familiar with where your current directory is somewhere in the file system?).
  2. It has a ton of command-line arguments to allow you to do some really complex directory searches on the file system with a very pithy command syntax.
  3. It outputs some useful details and aggregate information that is not part of the list of objects returned.
  4. It isn’t a standalone application that can be simply run from PowerShell.

Assume for example that you want to perform a recursive directory search for all files with a certain extension.  In cmd.exe, you would use a command syntax similar to this (varying on the extension, of course):

dir /s *.ps1

Running this command as is inside of PowerShell results in an error because the Get-ChildItem command (for which dir is an alias) doesn’t have a /s parameter.  All parameters in PowerShell are prefixed with a dash (-) instead of a slash (/), and the parameter for recursion in Get-ChildItem is -recurse (or -r), something that /s doesn’t necessarily translate to very intuitively.  Still, the help documentation you get from help dir is comprehensive, so you can quickly discover the -recurse parameter that way.  So now we have this:

dir -r *.ps1

When you run this in PowerShell, you won’t get an error anymore, but you will not likely get the results you expect either.  Personally, I know this has caused me to do some head-scratching a number of times.  The only ps1 files that will be listed are those that are in your current directory and those that are in a subdirectory (recursive) that ends with “.ps1”.  In other words, you most likely won’t get any results from this command.  Another visit to the help file and looking at the examples reveals that you actually meant to pass *.ps1 as the filter, not the path, so you need to do this:

dir -r -fi *.ps1

That’s not quite the same syntax as dir /s *.ps1 and it will definitely take some getting used to.  Also, if you were looking for the total amount of space consumed on the hard drive by these sorts of files like you would get in the results from cmd.exe, you won’t find that in PowerShell either.  This can be quite frustrating, especially to the newcomer.

Here’s another scenario: what if you want to do something simple like view all files in the current directory that are hidden?  It should be simple, right?  Here’s the cmd.exe syntax for that command:

dir /a:h

This command will also raise an error in PowerShell.  Once you get past the error and you learn the correct syntax, you’ll discover that the syntax in PowerShell, is a little more complicated, like this:

dir -fo | ? { $_.Attributes -band 2 }

Please note that the value of 2 is actually the integer value of [System.IO.FileAttributes]::Hidden and the ? is an alias for the Where-Object cmdlet.  In practice I would use the  [System.IO.FileAttributes]::Hidden enumeration instead of the value of 2 because I don’t have the attribute values memorized, but I am using pithy commands as short as I can make them to illustrate a point.

In both of these cases, as well as every other case I could come up with using dir, the old cmd.exe dir command syntax is shorter and it produces more information.  Plus, like many PowerShell users who have been using cmd.exe for a while, I already know the switches that are available in dir today.  There are a ton of cases like this because dir has so many switches that it supports, and these switches can be combined in many different ways to produce very useful results.

There are other issues with applying your knowledge of the dir command within PowerShell as well.  For example, with Windows Vista and Windows Server 2008, the dir command had some new switches added to it:

  • /r to show alternate file streams
  • /a:i to show files that are not content indexed
  • /a:l to show reparse points

Each of these switches works in Windows Vista and Windows Server 2008 but they don’t work in Windows XP or Windows Server 2003.  And by default, they don’t work in PowerShell either.

What do you do when facing differences like this that make it harder for you to get what you need from PowerShell?  Well, there are three options to choose from:

  1. Run back to cmd.exe when working with the file system because it already works (if it ain’t broke, don’t fix it).
  2. Translate the dir commands you use into PowerShell dir (or Get-ChildItem or gci) syntax and use them.
  3. Find a Poshoholic and ask him to help.

Personally I recommend the third option. 🙂

If you choose the first option you’re not learning PowerShell, you’re going back to oldschool cmd.exe (yuck!), you’re not getting rich .NET objects that you can work with in a pipeline, and you’re not getting support for the newer dir switches in downlevel operating systems like Windows XP and Windows Server 2003.  If you choose the second option, while that’s an admirable choice, there may be more work there than you think.  Really, the third option is the best.  And if you go with the third option, here are the sort of results you are going to get:

dir with tutorial in PowerShell

Over the last little while I’ve slowly put together a set of PowerShell extensions that will allow you to have full dir support in PowerShell like what is shown above on Windows XP, Windows Vista, Windows Server 2003 and Windows Server 2008, whether you’re running 32-bit or 64-bit.  This includes supporting for the following:

  1. Writing the PowerShell pipeline equivalent of dir to the host, regardless of which switches are used (this can be disabled).
  2. Full alternate file stream (AFS) support on demand; this includes retrieving alternate file streams, blocking or unblocking files, and adding or removing alternate data streams on any file or folder.  Even on Windows XP and Windows Server 2003 where dir in cmd.exe doesn’t support retrieving this information!
  3. Filtering options for files that are not content indexed and showing reparse points (not to mention any other attribute filters that are supported by dir) on all platforms supported by PowerShell.
  4. Recognition of and support for the dircmd environment variable.
  5. Support for every dir switch that is available today.
  6. Outputting detailed header and footer information just like you get from dir in cmd.exe.

See that green block of text in the screenshot?  That’s tutorial information, showing you the dir command you typed as well as the equivalent PowerShell command.  This is output no matter what your dir command is, unless you disable it by running “$PSTutorialDisabled = $true”.  Want the tutorial back?  Just remove the $PSTutorialDisabled variable or set it to $false.  Also if you noticed, the screenshot output includes detailed header and footer information plus integrated alternate file stream details like you get in dir.  That output is controlled by a script I wrote called Process-FileSystemChildItem (alias: pfsci), which you can see being called in the PowerShell equivalent script generated by the tutorial.  This modular support for outputting header and footer details as well as alternate file streams allows you to use Get-ChildItem and pipe the results to Process-FileSystemChildItem, selecting which output options are important to you.

What if you want to work with alternate file streams?  Looking at that list of files in the output above, removing the alternate file streams and showing header and footer information with the results is as simple as running this script:

gci *.ps1 `
    | % { if ($_.IsBlocked) { $_.Unblock() } } `
    | pfsci -showAlternateFileStreams

Want to block a file or folder?  Just do this:

(Get-Item $myItem).Block()

How about setting your own alternate file stream on a file or folder?  Here’s that syntax:

(Get-Item $myItem).WriteAlternateFileStream(‘Secret message’,’Poshoholic was here.’)

To retrieve the contents of that stream, it’s as simple as this:

(Get-Item $myItem).AFS[‘Secret message’].GetContents()

If either the true dir support or the AFS support interests you, you’ll need to download a few files and import them into PowerShell.  The required files along with their details for importing them into PowerShell are as follows:

ImportDirCmd.ps1
Requirements: dir.ps1, Process-FileSystemChildItem.ps1, Get-PSResourceString.ps1, FileStreams.types.ps1xml, and FileStreams.format.ps1xml

This file contains the required commands to load all of this functionality into PowerShell, provided that all items are in the same folder.  To use this functionality in PowerShell, simply dot-source this file after you have downloaded it and all required files.  This is the easiest way to extend PowerShell using this functionality.

dir.ps1
Requirements: Process-FileSystemChildItem.ps1

This file contains the script that processes the dir command you enter, parses it, generates the equivalent Get-ChildItem pipeline, outputs the tutorial information to the host and executes the command.  To use this command, simply invoke the script file directly using the call operator or dot-source the file and use the dir alias it creates.

Process-FileSystemChildItem.ps1
Requirements: FileStreams.types.ps1xml, FileStreams.format.ps1xml, and Get-PSResourceString.ps1

This file contains a script that supports outputting detailed header and footer information with pipelined file system items as well as alternate file stream information.  If the required type data and format data files are automatically loaded into the PowerShell session if they haven’t been loaded already.  To use this command, simply invoke the script file directly using the call operator or dot-source the file and use either the Process-FileSystemChildItem or the pfsci alias it creates.

Get-PSResourceString.ps1
Requirements: None

This file contains a script that supports loading localized resources to be used in PowerShell scripts.  This is a utility command often used when making parameters required, and therefore this file must either be placed in your path or it must be dot-sourced to create the Get-PSResourceString and grs aliases.  I use this internally when reporting certain errors back to the host, and I always call it by name without a path.

FileStreams.types.ps1xml
Requirements: None

This file contains the xml definitions for extending the PowerShell types to include types for alternate file streams, as well as extensions for FileInfo and DirectoryInfo objects so that they support file streams.  To use these extensions, import the file as follows:

Update-TypeData .\FileStreams.types.ps1xml

If you don’t update the type data manually, when you call Process-FileSystemChildInfo the type data will be updated automatically as long as the file is in the path or in the same directory as the Process-FileSystemChildInfo.ps1 script.

FileStreams.format.ps1xml
Requirements: None

This file contains the xml definitions for formatting alternate file streams in PowerShell.  To use this formatting, import the file as follows:

Update-FormatData -PrependPath .\FileStreams.format.ps1xml

You must use -PrependPath to import this file correctly.  If you don’t update the format data manually, when you call Process-FileSystemChildInfo the format data will be updated automatically as long as the file is in the path or in the same directory as the Process-FileSystemChildInfo.ps1 script.

Once you’ve downloaded these files, unblocked them and imported them into PowerShell, dir should work as well for you (better in some cases) as it does in cmd.exe, and you should have additional management support for blocked files and alternate file streams in general through PowerShell itself.  And hopefully, you’ll learn more about PowerShell along the way!

Thanks for reading,

Kirk out.

P.S.  Thanks to /\/\o\/\/ (Marc van Oursow) for the work he did with alternate file streams in this post and for the related files he included in PowerTab.  This work was my starting point for the support for alternate file streams.  My extension here differs slightly from his in that I don’t use a dll (this allows me to support both 32-bit and 64-bit platforms), my method signatures are slightly different and I added support for folders since they can also have streams added to them, but his work gave me a big head start on this.

Share this post:

 

PowerPack Challenge now open!

I mentioned earlier that there is a PowerGUI contest going on this summer called the PowerPack Challenge.  Well as of July 1st (Happy Canada Day everyone!), this contest is officially open and participants can start submitting their entries!

Interested in the details?  Read all about it here!

I look forward to seeing your contest entries!

Kirk out.

Share this post:

Book Review: Windows PowerShell Pocket Reference

While attending the IT Pro week of image TechEd 2008, I won my pick of a book from a table of 30 or so to choose from.  I noticed a few copies of Lee Holmes‘ new PowerShell book called ‘Windows PowerShell Pocket Reference‘ tucked in the middle and decided to pick one up.  Sure, it would only have cost me $12.99 Cdn to buy it and the other books I had to choose from were a $60 value or more, but I’m a Poshoholic, what else can I say?

To put it simply, I’m thrilled that I decided to pick up this book off the table.  Good things come in small packages.  I’ve only had Lee’s book for a little over a week and it has already provided me with enough value that I would even be satisfied if I had paid more than two times the list price.  Here are a few key reasons why I just love this book:

  1. It’s literally a pocket book, about the size of a short novel.  It’s lightweight and doesn’t take much space in my laptop bag, so I’ll be carrying this book with me everywhere for quite a while.
  2. It only briefly (19 short pages) introduces PowerShell and the rest is all meaty reference material that complements the documentation that is baked into PowerShell very nicely.
  3. Within 1 week I’ve already been able to use this book to quickly look up some things that either aren’t included in the baked in documentation or that aren’t detailed enough in the baked in documentation, and I’ve already discovered a few things that I wasn’t aware of that I could do with PowerShell because of this book.
  4. I can find what I’m looking for in the book very quickly by just flipping through the pages.

I should note that you shouldn’t look to this book to give you all the help you need on every cmdlet.  There’s no need for that because the PowerShell documentation already has lots of information on that front, and there are already a number of books that cover most cmdlets in detail.  But if you’re like me you  don’t want that information in a book like this anyway, because that would make it too heavy and reduce its usefulness as a quick reference.  If you are looking for fast access to cool things like PowerShell regular expression syntax, statements, operators, .NET string formatting options, variables, and details on certain really useful cmdlets like Add-Member and the Format-* cmdlets and useful .NET and WMI classes, then this book is for you.

Right now this is absolutely my favorite book format.  I don’t have time to do very much reading these days, and this book is great in that regard because it cuts right to the chase and gives me what I want in very little time, which fits my schedule quite nicely.  I’d love to have a book in a similar format for using PowerShell to manage and automate Exchange 2007, System Center Operations Manager 2007, System Center Virtual Machine Manager 2007/8, SQL Server 2008, or any other platform that has PowerShell support.  Skip the introductory portion (or minimize it), throw out all of the unnecessary details, and give me the meat in a small package that I can carry with me.  Modularity in books like you can find in well written software.  Sure, you won’t cover everything, but it should cover enough so that I’ll get valuable information that I can use in the little time that I have, and it should be portable.  That’s what I’m after

Aside: Ever see Japanese technical books?  They are much smaller than what we find here in North America.  Everything is still made too big over here, books included.

If you’re in the market for a new PowerShell book and you already use PowerShell, I seriously recommend you take a look at this book.  If you’re new to PowerShell and plan to use it a lot I think it’s worthwhile for you too, but if you like to learn directly from books with lots of details and descriptive examples you may want another book as well.  Then you can keep this book with you and leave the heavier one in your office.

Kirk out.

Share this post:

Scripting/Sysadmin meme

Andy Schneider called me out on the Scripting/Sysadmin meme that is circulating right now.  The questions are interesting and worth sharing to readers who want to get to know me better, so count me in!

How old were you when you started using computers?

9

What was your first machine?

A TRS-80.  Oh how I long for the days of fast-forwarding the tape drive until the analog counter reached the position of the program (game) I wanted to run.  The only game that I still remember is Scarfman.  After that it was an IBM PCjr, where I discovered King’s Quest (on a cartridge even, before it was labelled King’s Quest I).

What was the first real script you wrote?

I started learning Basic by writing one-liners that I would learn about in magazines like Compute!  These weren’t quite the same as PowerShell one-liners you find today though.  Then I would evolve those one-liners into more complex scripts as I expanded my knowledge of Basic.  If I recall correctly I think my first real Basic script beyond the one-liner (or several-liner) was one that displayed a menu with a list of song titles and it would play the song you selected (that I had encoded into script by hand), while changing background colors as the song played.

What languages have you used?

Basic, C, Pascal, SmallTalk, Lisp, C++, Delphi, Fortran, Visual Basic, C#, java, VBScript and PowerShell.

What was your first professional Sysadmin gig?

Well, technically I’ve never been hired as a Sysadmin.  My first related full-time job was while I finished University.  I started working at FastLane Technologies Inc., where I worked on FINAL, a scripting language for Sysadmins.  You can read more about that here.

If you knew then what you know now, would you have started in IT?

Honestly it’s hard to say.  Over the past 10 years while working in IT I’ve developed a strong passion for the environment, so while I love PowerShell I’d definitely be investigating both IT and environmentally friendly career alternatives.  Plus I love presenting, so maybe I’d be going after some combination of the three: presenting green IT solutions to sysadmins and showing them how to automate them using PowerShell.  Actually, I did that a presentation about that last week as part of the Speaker Idol competition at the TechEd IT Pro week (minus the PowerShell part…it was a 5 minute presentation after all).  I’ll blog more about that soon.

If there is one thing you learned along the way that you would tell new Sysadmins, what would it be?

Participate in the community!  Kai Axford and I talked about this to attendees of the EnergizeIT Certification Bootcamp User Group Tour last month…if you’re not participating in the community online and offline, attending your local user group meetings, and meeting other like-minded individuals around you, then you’re missing out on a huge opportunity.

What’s the most fun you’ve ever had scripting?

I’d have to say teaching myself PowerShell was by far the most fun I’ve ever had scripting.  There is just so much you can do with PowerShell, every day I was encountering multiple “Wow” moments as I grew my knowledge of PowerShell.

Who are you calling out?

Dmitry Sotnikov

Don Jones

Jeffery Hicks

Kai Axford

Thanks Andy for calling me out on this.  It was fun.

Kirk out.

Technorati Tags: ,,

Share this post:

PowerGUI wins Best of Tech Ed 2008 IT Professional Breakthrough Product award!

Well color me a happy camper!  Windows IT Pro just announced that PowerGUI has won the Best of Tech Ed 2008 IT Professional Breakthrough Product award!  Breakthrough Product is described by Windows IT Pro as the best single product of the Tech Ed 2008 IT Professionals conference, and could be from any IT Pro award sub-category.  Do yourself a favor and download PowerGUI today!  Even better, use PowerGUI to enter the PowerPack Challenge contest!

Kirk out.

Share this post:

Why isn’t my PowerPack showing up in PowerGUI?

Here’s an interesting question that came up recently:

“I just installed PowerGUI with the Exchange PowerPack and then I installed the Exchange 2007 Management Console.  The Exchange folder isn’t showing up in the PowerGUI Console.  Can you help?”

Yes, I can help.  Actually I came across this very same scenario myself not too long ago when working with a new VM.

If you install PowerGUI today, you can let it pick which of the core PowerPacks to install.  By default, PowerGUI will pick the PowerPacks for which you have the required PowerShell snapin(s) already installed.  For example, this means that the Exchange 2007 PowerPack will be installed automatically if you have the Exchange Management Console installed on your local machine, and the Active Directory PowerPack will be installed automatically if you have the Quest AD Cmdlets installed on your local machine.

Alternatively, you can also pick which PowerPacks you want to install yourself whether you have the required snapin(s) or not by using the custom install option.  I usually take this approach when I install PowerGUI, picking all PowerPacks so that I get all of the functionality that it comes with.  Most of the time this works fine when I have already installed all of the snapins I need, but what happens if you are missing one or more of the required snapins?  When you open PowerGUI for the first time your PowerGUI tree will automatically load all of the PowerPacks you installed for which you have the required snapins.  If you installed PowerPacks for which you don’t have the required snapins, they won’t show up in the PowerGUI tree.

Sounds like an easy enough problem to solve, right?  You can just install the missing snapin and then come back into PowerGUI and…um…notice that the PowerPack isn’t there.  This is because the automatic loading of the PowerGUI tree is only done once (at the moment; this will likely change in the future).  If you don’t have the required snapin installed when that happens, the PowerPack doesn’t get loaded into the PowerGUI tree.  Here lies the problem for the individual who asked the question stated earlier.

Fortunately the solution is straightforward once you know where the missing PowerPack is stored.  When PowerGUI is installed, all PowerPacks that are installed with it are placed in the PowerPacks subfolder of the PowerGUI installation folder.  To load a PowerPack that is missing because the prerequisite snapin wasn’t installed earlier, all you need to do is select File | PowerPack Management, click Import, browse to that folder and select the PowerPack that you want to import.  Once PowerGUI has verified that you have the required snapin(s) for that PowerPack installed it will import the PowerPack and your missing folder will be available in the PowerGUI tree.  And in case you weren’t aware, this is also the same way you would extend PowerGUI with other PowerPacks that you download from the library…download the PowerPack file, right-click the root node, select Import, and then select the PowerPack file you downloaded.

Hopefully this will help as you start using more snapins to manage and automate more with PowerShell and PowerGUI.

Kirk out.

Share this post:

PowerShell presence at TechEd 2008 IT Pro

I’m absolutely thrilled at the presence that PowerShell has had at the IT Pro week of TechEd 2008.  There have been a huge number of breakout sessions and interactive theatres talking about PowerShell and showing how to use it to manage your environment, many of which have been full or close too it (especially the interactive theatres — they were jam packed, standing room only in the back).  There’s also a PowerShell demo pod in the Management (blue) area and many vendors who have products in the PowerShell space ready to answer questions, show off demos, and in some cases give away free products too!  I’m sure Jeffrey Snover must be really happy (probably an understatement) with the well deserved interest and attention that PowerShell is getting.  This is such a fun space to work in, and PowerShell is absolutely the best technology I have ever worked with!

Kirk out.

Share this post:

Interview with Dmitry Sotnikov about PowerShell and PowerGUI on IT TV

Windows IT Pro magazine recently launched their new IT TV website, a community site that hosts videos relevant to today’s IT professional.  One video that caught my interest today is an interview with Dmitry Sotnikov discussing PowerShell and PowerGUI.  Curious about what PowerGUI is and how it might help you?  Check out the interview to learn more about it!

Kirk out.

Share this post:

Enter the PowerPack Challenge!

Ok, I’m really excited about this one.  Quest Software (my employer and sponsor of all PowerGUI development) has created a PowerGUI contest!  Whether you are a newcomer to PowerShell, a seasoned PowerShell scripter or somewhere in between, why not experiment with all of the creative things you can do with PowerGUI and PowerShell and enter the PowerPack Challenge!  All you need to do to enter is create and/or update one or more PowerPacks during the contest period (between July 1, 2008 and September 30, 2008).  There are some great prizes to be won, as described on the PowerPack Challenge contest page!

So now that you know about the contest, what should you do next?  Since the contest period doesn’t start until July 1, 2008, you have some time to prepare yourself for the contest.  if you aren’t totally familiar with PowerPacks or how to create them, you can learn how to do that by watching the flash demo I created here.  If you want to browse existing PowerPacks to get an idea what is already available, you can look in the PowerPack library here.  Or if you are attending the IT Pro week of TechEd 2008, there are a lot of sessions, hands on labs, instructor led labs, and more with PowerShell content and you can learn more about PowerShell and PowerGUI by visiting the demo station in booth 1101.

And please, just like anything else I post on this blog, if you have questions don’t hesitate to ask.

Thanks and good luck with your contest entries!

Kirk out.

Share this post:

Learn about PowerShell and PowerGUI at TechEd 2008

Are you interested in PowerShell?  Would you like to see a demo of some cool free tools that can help you use PowerShell more easily today?  Visit the PowerShell demo station in booth 1101 at TechEd 2008 and learn how PowerShell and PowerGUI can help you manage your IT environment more easily!

Kirk out.

Share this post: