Powershell errors with apostrophe in path - by Naf3tS

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 278008 Comments
Status Resolved Workarounds
Type Bug Repros 4
Opened 5/21/2007 3:18:57 PM
Access Restriction Public


The MyDocuments folder on my workstation is set to "C:\Stefan's Documents". My Powershell command prompt shortcut is configured to start in this folder (i.e. the "Start In" property of the shortcut).Windows PowerShell. Whenever I start a Powershell command prompt I get the following error:

Copyright (C) 2006 Microsoft Corporation. All rights reserved.

Incomplete string token.
At line:1 char:27
+ set-location 'C:\Stefan's D <<<< ocuments'
PS C:\Stefan's Documents> 

This is pretty annoying. I've tracked the bug down to the following line in the Microsoft.Powershell.ConsoleHost.InitializeRunspace:

string command = StringUtil.Format("set-location '{0}'", Environment.CurrentDirectory);

The resultant command is "set-location 'C:\Stefan's Documents'". The apostrophe in the directory name has not been escaped properly. This issue gets worse if you create a profile in this directory (i.e. $profile = C:\Stefan's Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1).

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

Incomplete string token.
At line:1 char:27
+ set-location 'C:\Stefan's D <<<< ocuments'
Incomplete string token.
At line:1 char:17
+ . 'C:\Stefan`'s D <<<< ocuments\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
PS C:\Stefan's Documents>

In this case the code that runs profile includes a call to the EscapeTickCharacters function (see Microsoft.Powershell.ConsoleHost.RunProfile). This function prefixes an apostrophe (') with a backwards apostrophe (`). According to "get-help about_escape_character' (`') should escape the apostrophe. This second error message must therefore be a bug in the parser / tokenizer (probably StringTokenReader).
Sign in to post a comment.
Posted by Lee [MSFT] on 11/23/2015 at 12:51 PM
Thank you for your feedback. This has beed addressed in recent versions of PowerShell:

Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.

PS> pwd


Posted by kradski on 1/20/2012 at 1:35 PM
Wow, this has been open since 2007, and *still* no fix. Microsoft expects people to adopt powershell--outright forces them to with Exchange 2007 and 2010--but heaven forbid you're stuck with a legacy MyDocs name that includes an apostrophe--you'll literally never be able to make specific-user@allHosts and specific-user@singlehost profiles without overhauling your entire organization. Insane.

Asking people to change their entire organization and file path structure is lazy, to say the least, when this is a clear bug that could and and should be fixed by microsoft--quickly--by just escaping the frickin' paths.

There might even be a security hole in there somewhere, too, if you think about it, much like a SQL injection attack.

Posted by opellegr on 12/21/2010 at 8:23 AM
I really wish you would fix this. It stinks that I'm not allowed to use user-scoped profiles because you forgot users can specify their own profile paths. Is there any timeframe for a fix? It's been 3 years since this was reported and a year since PSH 2.0.
Posted by Microsoft Connect on 9/25/2009 at 6:18 PM
Your bug has not been fixed in PowerShell 2.0. However we have kept the bug active for next version