Save WLAN Profiles names to a txt file

Executing the "netsh wlan show profiles" command I need to save the names of wifi profiles to a txt file for policy wifi and another one for user wifi.

I am using the following code;

@echo off &setlocal
set "flag="
(for /f "tokens=1*delims=:" %%a in ('netsh wlan show profiles') do (
    if "%%a"=="User profiles" set flag=true
    if defined flag if "%%~b" neq "" (
        for /f "tokens=*" %%c in ("%%~b") do echo(%%c
    )
))>out.txt
type out.txt

Open in new window


This only works for user profiles  and not group policy profiles.

netsh wlan show profiles

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
    LOGISTICS

User profiles
-------------
    <None>


C:\Users\dmarrow\Desktop>

Open in new window


netsh wlan show profiles

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
    DISDWIFI-FCNS

User profiles
-------------
    HOMEWIFI


C:\Users\dmarrow\Desktop>

Open in new window



Even when I change it to this code, nothing works

@echo off &setlocal
set "flag="
(for /f "tokens=1*delims=:" %%a in ('netsh wlan show profiles') do (
    if "%%a"=="Group policy profiles (read only)" set flag=true
    if defined flag if "%%~b" neq "" (
        for /f "tokens=*" %%c in ("%%~b") do echo(%%c
    )
))>out.txt
type out.txt

Open in new window

Dave LeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
PowerShell:
$section = $null
$profiles = Switch -Regex (& netsh.exe wlan show profiles) {
	'^Group policy profiles'	{$section = 'GroupPolicyProfile'; Break}
	'^User profiles'			{$section = 'UserProfile'; Break}
	'^\s+((?<Type>.*?)\s*:\s*)?(?<Name>.*)\s*$' {
		If ($section) {
			$profile = [PSCustomObject]([ordered]@{'Name' = $Matches['Name']; 'Type' = $null})
			If ($section -eq 'GroupPolicyProfile') {
				$profile.Type = 'GroupPolicy'
			} Else {
				$profile.Type = If ($Matches['Type'] -eq 'All User Profile') {'AllUsers'} Else {'CurrentUser'}
			}
			If ($profile.Name -ne '<None>') {
				$profile
			}
		}
	}
}
$profiles | Where-Object {$_.Type -eq 'GroupPolicy'} | Select-Object -ExpandProperty Name | Set-Content -Path 'C:\Temp\out_gp.txt'
$profiles | Where-Object {$_.Type -ne 'GroupPolicy'} | Select-Object -ExpandProperty Name | Set-Content -Path 'C:\Temp\out_user.txt'

Open in new window

Dave LeeAuthor Commented:
No luck on this one. If it helps here is the netsh command output

H:\>netsh wlan show profiles

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
    LOGISTICS-Managed

User profiles
-------------
    LOGISTICS


H:\>

Open in new window


However script needs to save any wlan profile now,  Should I change the following

	'^Group policy profiles'	{$section = 'GroupPolicyProfile'; Break}
	'^User profiles'			{$section = 'UserProfile'; Break}

Open in new window


to

	'^Group policy profiles'	{$section = 'Group policy profiles (read only)'; Break}
	'^User profiles'			{$section = 'User Profiles'; Break} 

Open in new window

oBdACommented:
No luck on this one
Meaning what exactly?
The script will not generate console output; it will create the two files defined at the end of lines 19 and 20.

Which OS is this?
Is the output under "User Profiles" really listed like this, only the name, without a leading "All User Profile     : " or "Current User Profile     : "?
If so, then the batch script shouldn't have returned any results,.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Dave LeeAuthor Commented:
Yes that is correct, it is not created the text files. OS in Windows 10.  Saved script as powershell.
oBdACommented:
This should work (and report to the console as well):
$section = $null
$profiles = Switch -Regex (& netsh.exe wlan show profiles) {
	'^Group policy profiles'	{$section = 'GroupPolicyProfile'}
	'^User profiles'			{$section = 'UserProfile'}
	'^\s+((?<Type>.*?)\s*:\s*)?(?<Name>.*)\s*$' {
		If ($section) {
			$profile = [PSCustomObject]([ordered]@{'Name' = $Matches['Name']; 'Type' = $null})
			If ($section -eq 'GroupPolicyProfile') {
				$profile.Type = 'GroupPolicy'
			} Else {
				$profile.Type = If ($Matches['Type'] -eq 'Current User Profile') {'CurrentUser'} Else {'AllUsers'}
			}
			If ($profile.Name -ne '<None>') {
				$profile
			}
		}
	}
}
$profiles
$profiles | Where-Object {$_.Type -eq 'GroupPolicy'} | Select-Object -ExpandProperty Name | Set-Content -Path 'C:\Temp\out_gp.txt'
$profiles | Where-Object {$_.Type -ne 'GroupPolicy'} | Select-Object -ExpandProperty Name | Set-Content -Path 'C:\Temp\out_user.txt'

Open in new window

Dave LeeAuthor Commented:
This worked great, thanks! Anyway to output current connected WLAN to textfile wether it's through Group Policy or User configured?
oBdACommented:
Try this:
$section = $null
$profiles = Switch -Regex (& netsh.exe wlan show profiles) {
	'^Group policy profiles'	{$section = 'GroupPolicyProfile'}
	'^User profiles'			{$section = 'UserProfile'}
	'^\s+((?<Type>.*?)\s*:\s*)?(?<Name>.*?)\s*$' {
		If ($section) {
			$profile = [PSCustomObject]([ordered]@{'Name' = $Matches['Name']; 'Type' = $null})
			If ($section -eq 'GroupPolicyProfile') {
				$profile.Type = 'GroupPolicy'
			} Else {
				$profile.Type = If ($Matches['Type'] -eq 'Current User Profile') {'CurrentUser'} Else {'AllUsers'}
			}
			If ($profile.Name -ne '<None>') {
				$profile
			}
		}
	}
}
$profiles | Out-Default
$interface = [ordered]@{}
$interfaces = Switch -Regex (& netsh.exe wlan show interfaces) {
	'^\s+(?<Key>.*?)\s*:\s*(?<Value>.*?)\s*$' {
		$interface[$Matches['Key']] = $Matches['Value']
		If ($Matches['Key'] -eq 'Profile') {
			[PSCustomObject]$interface
			$interface = [ordered]@{}
		}
	}
}
$interfaces | Out-Default
If ($interface = $interfaces | Where-Object {$_.State -eq 'connected'}) {
	$connection = "$($interface.Profile): $(($profiles | Where-Object {$_.Name -eq $interface.Profile}).Type)"
} Else {
	$connection = '<None>'
}

$profiles | Where-Object {$_.Type -eq 'GroupPolicy'} | Select-Object -ExpandProperty Name | Set-Content -Path 'C:\Temp\out_gp.txt'
$profiles | Where-Object {$_.Type -ne 'GroupPolicy'} | Select-Object -ExpandProperty Name | Set-Content -Path 'C:\Temp\out_user.txt'
$connection | Set-Content -Path 'C:\Temp\out_wlan.txt'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave LeeAuthor Commented:
What's the best method to disable console output? Only thing I need output of is the txt created on in the files.

C:\Temp\out_gp.txt
C:\Temp\out_user.txt
C:\Temp\out_wlan.txt

In my environment I will need to three seperate scripts for each individual txt file and the the output of each individual file will be show on the console.
oBdACommented:
Just comment out or remove lines 19 and 30 ("... | Out-Default")
Dave LeeAuthor Commented:
Okay thanks. I commented out those lines and added the following at the end

Get-Content -Path C:\Temp\out_wlan.txt

Open in new window


Works great, thank you.
oBdACommented:
Glad I could help.
Once you have your solution, you need to accept the comment(s) that solved your question:
How do I close my question?
http://support.experts-exchange.com/customer/en/portal/articles/2527982-how-do-i-close-my-question
Dave LeeAuthor Commented:
Thank you so much for helping tackle this. This is better than my last powershell script and gives me better insight into our wireless environment,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.