windows 11 %USERPROFILE% variable not working in admin powershell


cokedude

Active member
Member
Local time
7:34 AM
Posts
61
OS
10 and 11
I am having trouble with windows 11 %USERPROFILE% variable not working in admin powershell.

I was google searching windows 11 Save all wifi passwords to a text file. One of the ai answers were.

Code:
netsh wlan show profiles | Select-String "\:(.+)$" | %{$name=$_.Matches.Groups[1].Value.Trim(); netsh wlan show profile name="$name" key=clear | Select-String "Key Content\W+\:(.+)$" | %{$pass=$_.Matches.Groups[1].Value.Trim(); "$name : $pass"}} > %userprofile%\Desktop\wifi_passwords.txt

It complained:

Code:
PS C:\Users\ghost> netsh wlan show profiles | Select-String "\:(.+)$" | %{$name=$_.Matches.Groups[1].Value.Trim(); netsh wlan show profile name="$name" key=clear | Select-String "Key Content\W+\:(.+)$" | %{$pass=$_.Matches.Groups[1].Value.Trim(); "$name : $pass"}} > %userprofile%\Desktop\wifi_passwords.txt
out-file : Could not find a part of the path 'C:\Users\ghost\%userprofile%\Desktop\wifi_passwords.txt'.
At line:1 char:1
+ netsh wlan show profiles | Select-String "\:(.+)$" | %{$name=$_.Match ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

So then I wanted to make sure %USERPROFILE% was set and for some reason it is not set in the admin powershell.

Code:
PS C:\Users\ghost> echo %USERPROFILE%
%USERPROFILE%
PS C:\Users\ghost>

When I do windows run %USERPROFILE% it opens the %USERPROFILE% folder so why in the admin powershell will it not work?

I also found this


This command ran fine.

Code:
(netsh wlan show profiles) | Select-String "\:(.+)$" | %{$name=$_.Matches.Groups[1].Value.Trim(); $_} | %{(netsh wlan show profile name="$name" key=clear)}  | Select-String "Key Content\W+\:(.+)$" | %{$pass=$_.Matches.Groups[1].Value.Trim(); $_} | %{[PSCustomObject]@{ PROFILE_NAME=$name;PASSWORD=$pass }} | Format-Table -Wrap | Out-File "$env:userprofile\Desktop\WirelessNetworkPasswords.txt"

Why does userprofile seem to work but %USERPROFILE% does not work in the admin powershell?

I found a superuser post that said not to use %USERPROFILE% with no explanation.


mkdir C:\Users\%USERPROFILE%\AppData\Roaming\modinstaller\recovery<br>

use this:


mkdir $home\AppData\Roaming\modinstaller\recovery<br>
 

My Computer My Computer

At a glance

10 and 11
OS
10 and 11
Computer type
Laptop
Percents are not how PowerShell handles environment variables. The second command works not because it is using "userprofile," but because it is using $env:UserProfile, which is correct PowerShell syntax.

Of course the user's desktop is not always inside the user profile, so the correct thing to do would be

[System.Environment]::GetFolderPath([Environment+SpecialFolder]::DesktopDirectory)
 

My Computer My Computer

At a glance

Windows 11 Enterprise 25H2 [rev. 8893]
OS
Windows 11 Enterprise 25H2 [rev. 8893]
Out-File "$([Environment]::GetFolderPath("Desktop"))\WirelessNetworkPasswords.txt"
 

My Computers My Computers

  • At a glance

    11 Homei7 13650HX16GB DDR5GeForce RTX 4060 Mobile
    OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
    Antivirus
    What's an antivirus?
  • At a glance

    11 Homei5 1135G716GB DDR4Intel Iris Xe
    Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    30Mbit/s up, 500Mbit/s down
    Browser
    FF
Back
Top Bottom