Problem with pipelining

 Get-ChildItem -path 'c:\r_sps\settings\*.BACKUP' -file | Measure-Object | {$_.Count} 

works fine. But if I call it from batch via:

powershell -ExecutionPolicy "ByPass" -Command " Get-ChildItem -path 'c:\r_sps\settings\*.BACKUP' -file | Measure-Object | {$_.Count} "

it gives me " Expressions are only allowed as the first element of a pipeline"

What’s wrong?

Many thanks - Michael

Variables expand in double quotes. I would switch the quotes to have the outer most quotes single quotes.


powershell -ExecutionPolicy "ByPass" -Command ‘Get-ChildItem -path “c:\r_sps\settings\*.BACKUP” -file | Measure-Object | {$_.Count}’

The other fix would be to escape the dollar sign for any variable that shouldn’t expand.

Where-Object {`$_.count}

Thanks! But neither

powershell -ExecutionPolicy "ByPass" -Command " Get-ChildItem -path '%_R_SPS%\settings\*.BACKUP' -file | Measure-Object |  {`$_.Count } "

nor

powershell -ExecutionPolicy "ByPass" -Command 'Get-ChildItem -path "c:\r_sps\settings\*.BACKUP" file | Measure-Object | {$_.Count}'

did work. Which qoute did you use in 1st proposal: Copying ‘ gives me strange results. I changed it to ’ but also there no success.Can you comprehend my problem (with any directory of your choice)?

I could solve it with a script but a single command would be more vonvenient for integration into my batch system.

Michael

According to the help you get when you run

PowerShell /?

I would try this way:

PowerShell.exe -EP Bypass -Command "& {(Get-ChildItem -Path 'c:\r_sps\settings\*.BACKUP' -File).count}"

I might add the parameters -NoProfile and -NoLogo as well. :wink:

Retype the quotes. I did it on my phone so chances are they are special quotes.

Many thanks: It runs without error! But now I have a followup question: My expectation was that the DOS-ERRORLEVEL would reprepresent the result of the statement (in my case : 5 for 5 files found). Instead ERRORLEVEL is 0 (zero).
Any way to get the result into the calling DOS batch (except using a script or writing (OUT) it into a textfile)?
Michael

First of all: There is no “DOS” anymore for more than 20 years! It is the “command prompt”, “CMD” or maybe “batch file” … but no “DOS”. :smirk:

The general recommendation is: “Do not use ancient batch files anymore!” PowerShell is mor than capable to replace any possible batch file because it is way more advanced and powerful.

Your expectation is wrong. The “ERRORLEVEL” is genereted by the PowerShell process itself - not the script running in it. And there are two defaults. “0” for success and “1” for failure. You can advice PowerShell to return a diffent numeric error level by using the keyword Exit followed by the number you want.

Again - don’t use batch files anymore. Migrate them to native PowerShell.

Sorry for historic wording on DOS / batch etc. , but the envíronment semmed to be clear.

Migration: My batch system consists of about 2.200 KB of code. Migration would be a too big effort doing it at once. So my concept is doing it step by step.

Obviously I cannot include an exitcode into a command as above?

So I have to use to a PS-script?

Thanks - Michael

The sooner the better. :+1:t4: :wink: :slightly_smiling_face:

I’d expect it to shrink actually when migrated to PowerShell because of the advanced language features. And you’ll very likely find easier help for PowerShell. :wink:

PowerShell.exe -EP Bypass -NoLogo -NoProfile -Command "& {(Get-ChildItem -Path 'c:\r_sps\settings\*.BACKUP' -File).count}"

Of course I use another path but when I run this snippet I get a number back?! :man_shrugging:t4:

Thanks and I know: It’s only a little step on may wax from batch to PS.

PowerShell.exe -EP Bypass -NoLogo -NoProfile -Command "& {(Get-ChildItem -Path 'c:\r_sps\settings\*.BACKUP' -File).count}"

works fine but I have to retransfer the result into batch. In general the way is (example from [windows - BATCH — Store command output to variable - Stack Overflow]):

for /F %%a in ('@TASKLIST ^| FIND /I /C "%_process%"') do @SET "_count=%%a"

but using

for /f %%i in (' PowerShell.exe -EP Bypass -NoLogo -NoProfile -Command "& {(Get-ChildItem -Path c:\r_sps\settings\*.BACKUP -File).count}" '} do echo %%i

gives strange results (the current directory in the commandline changes).

Michael

I never had to get familiar with CMD/Batch in depth and I refuse to start with it now. :wink: Especially when it comes to loops and variables I think the syntax of CMD/Batch is just atrocious and I don’t like to think about it. :man_shrugging:t4: Sorry.

I fully understand.

I solved the problem by using usebackq (back quotes):

for /F usebackq %%i in (`PowerShell.exe -EP Bypass -NoLogo -NoProfile -Command "& {(Get-ChildItem -Path c:\r_sps\settings\*.BACKUP -File).count}" `) do  echo Result: %%i

And yes: I shall go my way from batch to PS.

Many thanks - Michael