I am enclosing the file path in single quotes, because it may contain spaces.
It worked fine, until I encountered a file path containing square brackets, which seem to be interpreted as a wildcard character. I managed to solve it by adding the -literalPath parameter :

So far, so good ...
But file paths may also contain single quotes, dollar signs, ampersands, ... and all these characters seem to have a specific function in powershell for which the -literalPath parameter does not seem to work.
I tried enclosing the path with double quotes or escaping with the `character, but that did not solve my problem either :-(

Any suggestions on how to pass a file path to Powershell which may contain spaces, single quotes, square brackets, ampersands, dollar signs, etc. ?

Update:
Someone here already showed me how to get it working from within Powershell, but somehow the answer has been removed ?
Anyway, I did create a file called $ & ' [].txt.
This works form within Powershell (needed to escape the &) :

Could you provide a few examples of paths which are not working, and the exact error you get? You have only provided working paths so far, so it's hard to tell where the problem is.
–
latkinAug 23 '12 at 14:21

I updated my question and added some working and non-working examples. As you can see, I can get it to work from within the Powershell prompt, but not by executng powershell from the Windows command prompt.
–
jeroen_de_schutterAug 23 '12 at 15:11

I reproduced your issue by creating a file named "$ * ' [].txt". Then, from a powershell prompt, I typed get-item <tab> and powershell completed the filename for me, but I can't paste the completed filename in here because markdown eats all the special characters. Give it a try and see if that helps.
–
joXnFeb 5 '13 at 22:54

2 Answers
2

If you can write the path to a temporary txt file, the following works OK:

(get-item -literalpath (gc 'C:\Temp\path.txt')).creationTime

The file @ C:\Temp\path.txt contains the path with special characters in it, other than this I think you would have to escape each special character on a per path basis.

In addition to the hack above, it appears Powershell V3 may help out here with the addition of a new 'magic parameter' language feature. Specifically, see the section headed 'Easier Reuse of Command Lines From Cmd.exe' here and because I hate link-rot, here it is, shamelessly reproduced below:

Easier Reuse of Command Lines From Cmd.exe

The web is full of command lines written for Cmd.exe. These commands
lines work often enough in PowerShell, but when they include certain
characters, e.g. a semicolon (;) a dollar sign ($), or curly braces,
you have to make some changes, probably adding some quotes. This
seemed to be the source of many minor headaches.

To help address this scenario, we added a new way to “escape” the
parsing of command lines. If you use a magic parameter --%, we stop
our normal parsing of your command line and switch to something much
simpler. We don’t match quotes. We don’t stop at semicolon. We
don’t expand PowerShell variables. We do expand environment variables
if you use Cmd.exe syntax (e.g. %TEMP%). Other than that, the
arguments up to the end of the line (or pipe, if you are piping) are
passed as is. Here is an example: