windows 11 %USERPROFILE% variable not working in admin powershell


cokedude

Active member
Local time
10:29 PM
Posts
57
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

System One

  • 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

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
Out-File "$([Environment]::GetFolderPath("Desktop"))\WirelessNetworkPasswords.txt"
 

My Computers

System One System Two

  • 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?
  • 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