Reputation: 117
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
Upvotes: 0
Views: 1766
Reputation: 46690
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.
Upvotes: 2
Reputation: 4069
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.
Upvotes: 3