powershelluser
powershelluser

Reputation: 25

PowerShell Boolean Expression

I am writing a powershell script but having a problem evaluating a boolean expression.

This is the line of code I am having a problem with:

if (Get-Content .\Process2Periods.xmla | Select-String ((Get-Date) | Get-Date -Format "yyyyMM") -quiet -ne True)

I am getting this error message when trying to run:

Select-String : A parameter cannot be found that matches parameter name 'ne'.

Please help me understand the issue.

Also for a little context, I am searching a file for a string and if it doesn't exist I want to execute what is in the if block. I didn't paste the code in the if statement because I don't believe it is relevant but please let me know if you would like to see it.

Upvotes: 1

Views: 774

Answers (2)

user2555451
user2555451

Reputation:

PowerShell is interpreting the -ne as being a parameter for Select-String. To fix the problem, you can remove the -ne True part and use the -not operator instead:

if (-not (Get-Content .\Process2Periods.xmla | Select-String ((Get-Date) | Get-Date -Format "yyyyMM") -quiet))

Note that ! would also work if you prefer it over -not:

if (!(Get-Content .\Process2Periods.xmla | Select-String ((Get-Date) | Get-Date -Format "yyyyMM") -quiet))

Also, the (Get-Date) | Get-Date -Format "yyyyMM" part of the line above is unnecessary. You can instead just do Get-Date -Format "yyyyMM". See below:

PS > (Get-Date) | Get-Date -Format "yyyyMM"
201502
PS > Get-Date -Format "yyyyMM"
201502
PS > 

Upvotes: 4

Etan Reisner
Etan Reisner

Reputation: 80921

Your parenthesizing is off.

The -quiet and -ne arguments are being see as arguments to Select-String.

I'm unsure what command you wanted -quiet to apply to (I expect Select-String) but you need to wrap the entire Get-Content ... | Select-String ... bit in () and then use -ne "True" or -ne $True (depending on whether you want string or boolean true).

if ((Get-Content .\Process2Periods.xmla | Select-String ((Get-Date) | Get-Date -Format "yyyyMM") -quiet) -ne $True)

Upvotes: 1

Related Questions