I’m brand new to Powershell.
I’m writing a script to validate the basic functionality of Microsoft Edge. All it needs to do is open an msedge window and check to see that the correct default site loads.
I originally wanted to grab the URL and output it but I only see solutions that are way too lengthy for a script like this, so I opted to output the result of grabbing MainWindowTitle.
I am trying to use it like this:
if((Get-Process | Select MainWindowTitle) -like "wikipedia.org"){
Write-Host "Site successfully opened."
} else {
Write-Host "Site failed."
}
Despite landing on the correct site and the output of Get-Process | Select MainWindowTitle being identical to the string I compare it to, the test fails. The command works fine outside of an if statement so I guess it can’t be used in the way I’m attempting to use it. Is there any way to do this?
>Solution :
Select MainWindowTitle outputs an object with a single property.
Comparing an object with a string usually doesn’t work as expected (PowerShell tries to convert the object to string using its .ToString() method, which in case of a process object produces something like "System.Diagnostics.Process (processname)").
You may use Where-Object in its simplified form to filter for objects with given property values:
if( Get-Process | Where-Object MainWindowTitle -like '*wikipedia.org*' ) {
Write-Host "Site successfully opened."
} else {
Write-Host "Site failed."
}
The condition evaluates to $true, if Where-Object found at least one matching process object.