Manipulating string output

I am trying to use Get-Webbinding on a web server to see any bindings for all sites. Get-Webbinding | Select bindingInformation gives me the following output.

I want to drop the IP and port from the results so that it only shows the domain names like this…

I have attempted to use split for this but I cant seem to get this to loop through the array correctly. The below only gives me the results of the last in the array (

$bindings=get-webbinding | select bindingInformation | Out-String
foreach ($b in $bindings) {$b.Split(’:80:’)[-1]}

What am I missing here? Any help would be greatly appreciated.

Have you tried running your foreach loop first then piping to our-string?

I dont think I can use Split unless its a string first. I get this if I try…

Method invocation failed because [Selected.Microsoft.IIs.PowerShell.Framework.ConfigurationElement] does not contain a method named

$Binding = Get-WebBinding |Select-Object BindingInformation |Out-String
$Binding -replace '\*\:.*\:',''


This did not work; this gave me the same results as Get-WebBinding |Select-Object BindingInformation |Out-String. Nothing was removed from the original output.

Also, I tweaked what you provided and it does work for removing specific data (lets say the :80:) but i need to to removed everything before not just the port number as the IPs will be different everywhere I run this.

$Binding -replace [regex]'.*\:',''

This should remove everything except for the host header.


Excellent. This works! Thanks for your assistance.

No problem.

get-webbinding | select -expand bindingInformation | 
  foreach { ($_ -split '80:')[1] }

Below is what I put together for my purpose. This presents all of the host headers in a comma separated list.

$binding=(Get-WebBinding | Select-Object BindingInformation)
$removeip=@($Binding -replace [regex]’.*:’,’’)
$removechar=@($removeip -replace [regex]’}’,’’)
$removeduplicates=@($removechar | Select-Object -Unique) #this removes the duplicate headers listed from 80 and 443 bindings.
$removeduplicates -join “,”

The only other thing I would like to do is filter out anything that doesn’t have a subdomain so that I only see the www bindings and anything else that includes a subdomain (anything that has at least two periods in the results).

pwshliquori, can I also use a regex to do this in the get-bindings section? Something like the below?

(Get-WebBinding | Where {$_.BindingInformation -contains [regex]“INSERT SOME REGEX here?”} |Select-Object BindingInformation)

I think i figured it out…

$binding=(Get-WebBinding |Select-Object BindingInformation)
$removeip=@($Binding -replace [regex]’.*:’,’’)
$removechar=@($removeip -replace [regex]’}’,’’)
$removeduplicates=@($removechar | Select-Object -Unique)
$onlysubdomains=@($removeduplicates -match [regex]"[A-Za-z0-9-]+[.]+[A-Za-z0-9-]+[.]+[A-Za-z0-9-]")
$onlysubdomains -join “,”

Thanks again. That one regex example has helped me tremendously! Ill be using regex a lot more in future scripting.