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)?
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”.
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.
I never had to get familiar with CMD/Batch in depth and I refuse to start with it now. 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. Sorry.