简体   繁体   中英

Power shell foreach error

I was making a program on Powershell to list all the files above 1MB, between 0.5MB and 1Mb and smaller than 0.25MB. Here's my code:

$files = Get-ChildItem "C:\Temp" |`
Where {!$_.PsIsContainer}`
foreach ($file in $files){`
switch ($file.Length){`
{$_ -gt 1MB}{Write-Host $file.Name, $file.Length`
-ForegroundColor Red; break}`
{$_ -gt 0.5MB}{Write-Host $file.Name, $file.Length`
-ForegroundColor Magenta; break}`
{$_ -ge 0.25MB}{Write-Host $file.Name, $file.Length`
-ForegroundColor Cyan; break}`
default {Write-Host $file.Name, $file.Length}`
}`
}`

I'm getting the error from the third line( foreach ($file in $files) ). The error says:

Unexpected token 'in' in expression or statement. At line:3 char:18 + foreach ($file in <<<< $files){` + CategoryInfo : ParserError: (in:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

I'm a beginner right now with Powershell. Any help would be appreciated

foreach is both an alias to a cmdlet ( ForEach-Object ) and a keyword ( foreach ). When used in a pipe, PowerShell assumes the alias, which does not use in .

If you want to use it in the pipe as ForEach-Object , you'll need to use this syntax:

$test_array = @("this","that")

$test_array | foreach{Write-Output $_}

> this
> that

Otherwise, you need to use foreach as such:

foreach($thing in $test_array)
{
    Write-Output $thing
}

> this
> that

This does not take, or produce output on the pipe.

Note that these are two different commands, and the use of foreach as an alias to ForEach-Object can be confusing. Within the pipe, I'd recommend using the full cmdlet name.

ForEach(... in ...) is not meant to be part of a pipe or anything. It's stands on its own and should be coded as such. The way you have your code the backticks are combining lines that shouldnt be.

$files = Get-ChildItem "C:\Temp" |`
Where {!$_.PsIsContainer}`
foreach ($file in $files){`

Could also read as the following. Note that there are no separators/operators between the Where clause and the ForEach definition which is syntactically wrong and the source of your error.

$files = Get-ChildItem "C:\Temp" | Where {!$_.PsIsContainer} foreach ($file in $files){

Which is where you are having an issue. Instead the following looks cleaner and functions more like you intended.

$files = Get-ChildItem "C:\Temp" | Where {!$_.PsIsContainer} 

foreach ($file in $files){
    switch ($file.Length){ 
        {$_ -gt 1MB}{Write-Host $file.Name, $file.Length -ForegroundColor Red; break} 
        {$_ -gt 0.5MB}{Write-Host $file.Name, $file.Length -ForegroundColor Magenta; break} 
        {$_ -ge 0.25MB}{Write-Host $file.Name, $file.Length -ForegroundColor Cyan; break} 
        default {Write-Host $file.Name, $file.Length}
    } 
}

It is also a good practice to indent your code for readability. You also had line breaks at -ForegroundColor which were incorrect. Backticks in powershell are used as an escape character. They can be used to continue a piece of code onto a new line for readability and are not required on every line. Using them the way you did could concat code that was not meant to be. It was not wrong you just need to be careful. You had more errors then you mentioned but I addressed the others with some proper formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM