M
M
Michael Compass2022-02-14 17:20:35
PowerShell
Michael Compass, 2022-02-14 17:20:35

Why doesn't the PowerShell script run through the batch file?

I created a batch file that runs the following powershell script:

dir C:\Users\home64\Downloads\1a -file | sort -property name -Descending | %{$i=0} {$_.CreationTime = (Get-Date).AddDays($i); $i++}


Batch code:
@echo off
SetLocal EnableExtensions

%SystemRoot%\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy ByPass -command "dir C:\Users\home64\Downloads\1a -file | sort -property name -Descending | %{$i=0} {$_.CreationTime = (Get-Date).AddDays($i); $i++}"
pause


When I run the batch file, I get an error:
620a64bcd58dd716394642.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2022-02-14
@mik777em

In CMD, the % sign is interpreted as a command line option, while in Powershell it is an alias to Foreach.
Change the % to Foreach in the command and try to run the script again.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question