Home > Software design >  How can I get an item from an object without title in powershell?
How can I get an item from an object without title in powershell?

Time:04-07

I am trying to learn some powershell. I want to get only the SSID from a connected network.
My code is : netsh wlan sh int | select-string SSID | select -First 1.
The output is SSID : ABCDEFG. My output must be just ABCDEFG. SSID doesn't have a title so I don't know how I can select his value ABCDEFG.

My first question: How can I select the value ABCDEFG from my object SSID and put only the value in my output?
My second question: Is the output from my code always a connect network or not?

CodePudding user response:

netsh is a traditional command line tool. In contrast to PowerShell cmdlets, which output structured objects, it outputs lines of text.

Select-String gets you one of those lines (the one that has "SSID" on it).

You could split it at the colon and take the latter part.

$ssid_line = netsh wlan sh int | select-string SSID | select -First 1
$ssid = $ssid_line -split ':' | select -Last 1

Write-Host $ssid

Use $ssid.Trim() to remove any excess whitespace.

CodePudding user response:

You can use:

(Get-NetConnectionProfile).Name

which will return just the name of the profile as:

SSIDNAME

If there's more than one network connection then you'd need to specify which one you want.

You can grabbing the Wi-Fi interface names with:

(Get-NetConnectionProfile | Where-Object InterfaceAlias -eq "Wi-Fi").Name

which will also return the name of the profile (if found) as:

SSIDNAME

These are a few ways you can do it using strictly powershell's netadapter and netconnection.

https://docs.microsoft.com/en-us/powershell/module/netadapter/?view=windowsserver2022-ps

https://docs.microsoft.com/en-us/powershell/module/netconnection/?view=windowsserver2022-ps#netconnection

  • Related