Powershell + File names + Special Characters <> Frustration (Eventually)

Have you ever tried to work in Powershell with a file or directory name that has unusual charaters in it?  When I was new to Powershell I stumbled across this with the standard names my software-development group used for certain directories—they contained square brackets: [ and ].

If you haven’t already come across this, go ahead and try working with such a directory in Powershell.  Use Windows Explorer to create a directory named [PowershellTest].  Then, in Powershell, try to Set-Location (or CD) into your new directory.  No good.  You can try putting quotes around it too, but it won’t help.

image

Wasn’t that fun?  Frustrated yet?

Have no fear; there’s an answer.  Internally, Windows is still keeping track of the short file names in the “old fashioned” 8.3 format.  It seems that Powershell is using these internal names.

There are several good ways to get around the problem. 

  • Never use such names!  But that’s not always an option.

  • Use the short 8.3 name.  There’s a great article about this at:  http://support.microsoft.com/kb/142982.  Note that in this article there’s one thing not made clear.  It’s in the first bullet point, about stripping certain characters when Windows generates the short name.  It does NOT delete them; rather, it changes them to underscores.  So in my case (but it won’t ALWAYS be the same on your machine), the directory name translates into _POWER~1.  And this name works fine!

image

 

image

 

  • Use the –literalpath argument, available on some (but unfortunately not all) commands that work with file names.

image

  • Download Powershell Community Extensions from http://pscx.codeplex.com/Wikipage, and use the Get-ShortPath cmdlet.  Note that you’ll still need to use –literalpath with this cmdlet, but once you have the short name you can use it for any other operation you want.

usepscx

 

Hopefully one of these options will work for you!

 

Advertisements

One Response to “Powershell + File names + Special Characters <> Frustration (Eventually)”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: