Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grab Microsoft Edge Tab title/URL using Powershell

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading