I have a powershell command that runs fine.

$(Get-Item c:\0\01.txt).lastwritetime=$(Get-Date "01/22/2002 11:11 am")

I'd like to use it multiple time. Using a batch file. Before the batch file I used this:

Set-ExecutionPolicy Unrestricted

The batch contains commands like this one:

powershell -Command "$(Get-Item c:\0\01.txt).lastwritetime=$(Get-Date "01/22/2003 11:11 am")"

The batch file fails, I got two error messages per command:

Get-Date : A positional parameter cannot be found that accepts argument '11:11'.
At line:1 char:49
+ $(Get-Item c:\0\01.txt).lastwritetime=$(Get-Date <<<<  01/22/2003 11:11 am)
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetDateCommand

Exception setting "LastWriteTime": "Cannot convert null to type "System.DateTime"."
At line:1 char:25
+ $(Get-Item c:\0\01.txt). <<<< lastwritetime=$(Get-Date 01/22/2003 11:11 am)
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Unfortunately I don't know what is wrong. Can some help me out?

screenshot of powershell window

  • 1
    Replace one set of double "s with single quotes 's – DavidPostill Dec 11 '18 at 23:00

You need to either escape the inner double quotes with a backslash (because cmd would see the date time unquoted)

powershell -Command "(Get-Item .\01.txt).lastwritetime=(Get-Date \"01/22/2003 11:11 am\")"

or exchange the inner double with single quotes (as DavidPostill suggested).

powershell -Command "(Get-Item .\01.txt).lastwritetime=(Get-Date '01/22/2003 11:11 am')"

Also the $ are only neccessary when forcing the expression inside a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.