Use the environment variable $env:USERPROFILE.

For example:

cd "$env:USERPROFILE\Downloads"

will take you to the user's Downloads folder (given that it's named "Downloads").]

Answer from Nikita Karamov on Stack Overflow
🌐
Reddit
reddit.com › r/powershell › get userprofile path for current logged in user.
r/PowerShell on Reddit: Get userprofile path for current logged in user.
May 28, 2021 -

Hello Powershellers

In my current environment, there are several users who’s profile folder name is different than their username due to AD changes. So I’m looking for a way to find the profile path of the logged in user and I can’t necessarily rely on the username. The other twist is that the power shell script is run by third party software and I’m not sure which service it runs as because $env:Userprofile variable doesn’t work when using the software. Any other ideas on how I can do this? My end goal is to copy a shortcut that is under a users profile to the startup folder.

Discussions

Powershell script to get current user path using local system credentials
Hello, I am running a PS script on a remote computer to clear profile temp data and other folders using SOLARWINDS. Issue is that solarwinds executes script using local system account and not logged in user hence the profile are not correctly enumerated by $env variable. More on community.spiceworks.com
🌐 community.spiceworks.com
10
5
August 16, 2020
Powershell filepath for current user's documents folder?
I am trying to copy a file to any current/logged in user’s documents folder, but not sure if the below path is correct? C:\Users%user%\Documents Many thanks Tung More on forums.powershell.org
🌐 forums.powershell.org
7
0
June 10, 2022
powershell get current users path variable
Hi How I do get the PATH value only for a single user ? I have two user on my desktop and I want to change the PATH only for the ‘User1’ for example : $PATH = $env:Path $PATH += ";" + $jdkInstallFolder +"\bin" #The new folder to add Set-EnvironmentVariable -name PATH -Value $PATH -Target ... More on forums.powershell.org
🌐 forums.powershell.org
0
0
January 1, 2020
environment variables - PowerShell: Using $env:userprofile in an 'IF' statement - Stack Overflow
I am using PowerShell ISE (I think 4). I am writing logon scripts to replace the old '*.BAT' files. I am trying to test for a user-profile condition before 'creating/deleting' certain directories f... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › powertip: find user's home directory in powershell
PowerTip: Find User's Home Directory in PowerShell - Scripting Blog [archived]
February 18, 2019 - Summary: Find the path to the current user's home directory in Windows PowerShell. How can I easily find the current user's home directory for a script I am writing? Use the $home automatic variable: PS C:> $HOME
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › powertip: use powershell to find user profile path
PowerTip: Use PowerShell to Find User Profile Path - Scripting Blog [archived]
February 19, 2014 - Summary: Use Windows PowerShell to find the user profile path. How can I easily get information about the folder and path to the profile for a currently signed-in user? Use the Env: PowerShell drive, and select the UserProfile environmental variable: $env:USERPROFILE
Find elsewhere
🌐
PowerShell Forums
forums.powershell.org › powershell help
powershell get current users path variable - PowerShell Help - PowerShell Forums
January 1, 2020 - Hi How I do get the PATH value only for a single user ? I have two user on my desktop and I want to change the PATH only for the ‘User1’ for example : $PATH = $env:Path $PATH += ";" + $jdkInstallFolder +"\bin" #The new folder to add Set-EnvironmentVariable -name PATH -Value $PATH -Target ...
🌐
Seei
seei.biz › get-ad-user-home-directory-using-powershell
Get AD User Home Directory using PowerShell – Software Effect Enterprises, Inc
December 9, 2021 - We can easily retrieve AD user’s home directory path by using the Active Director powershell cmdlet Get-ADUser.
🌐
Delft Stack
delftstack.com › home › howto › powershell › powershell get current user
How to Get Current User in PowerShell | Delft Stack
February 2, 2024 - There are many methods to retrieve current users signed on to a machine without using PowerShell. Using PowerShell, you can also alter the results by treating them as objects. For example, if you execute a script asking for user credentials, PowerShell can dynamically generate the username, leaving you with only the password to enter manually. We will use PowerShell cmdlets, namely Get-CimInstance and Get-WMIObject.
🌐
Reddit
reddit.com › r/powershell › how to get current user's appdata folder within a script ran as system context
r/PowerShell on Reddit: How to get current user's AppData folder within a script ran as system context
February 19, 2025 -

Hello Expert!

I am running a powershell script in intune that run as system context. I need to copy folder to C:\Users\$currentuser\AppData\Roaming folder. Currently I am using below command to get current user logon info.

$currentUser = Get-WmiObject Win32_Process -Filter "Name='explorer.exe'" | ForEach-Object { $_.GetOwner() } | Select-Object -Unique -Expand User

any advice how can I complete this?

Thanks.

Top answer
1 of 2
2

I would look up the reg key for the user's profile folder you want to copy to eliminate ambiguity.

HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\[Users SID]\Profile Image Path

As for creating the home directory, that's the tricky part. if this is a completely new user, you can try Microsoft's USMT tool, specifically the loadstate exe, and create the account that way. I haven't used it this way but my first attempt would be to create a profile on some computer, then back it up with scanstate. Next, you can deploy it to any machine with loadstate, specifying the new account name as a parameter.

What loadstate does is automatically create the user's profile (including the profile folder, that you want to copy the files to), and it restores files that were backed up (which is minimal if you backup an empty or fresh profile), but the important part is that you get a profile folder created.

USMT is available as a download from MS and comes with the Windows 8 ADK also.

2 of 2
1

You just need to run a command as the new user to initialize a profile folder. Wrote a function that returns the users profile folder path.

function New-UserProfileFolder ([string] $username, [string] $password) {

    $sec_password = ConvertTo-SecureString $password -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $sec_password

    # Run command to create profile folder
    Start-Process cmd /c -WindowStyle Hidden -Wait -Credential $credential -ErrorAction SilentlyContinue

    # Get information from WMI
    $user = Get-WmiObject -Namespace root/cimv2 -Class win32_useraccount -Filter "LocalAccount=True AND Name='$username'"
    $userprofile = Get-WmiObject -Namespace root/cimv2 -Class win32_userprofile -Filter "SID='$($user.sid)'"

    $userprofile.localpath
}
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › get current directory or script path in powershell
Get Current Directory or Script Path in PowerShell - SharePoint Diary
November 23, 2021 - Here’s an example to get the current directory path using the Get-Location command: ... This example shows that the current Windows directory is C:\Users\Thomas. You can also use the alias “pwd” to get the current directory.
🌐
Rene Nyffenegger
renenyffenegger.ch › notes › Windows › dirs › Users › username › index
%USERPROFILE%
A possible way to determine the ... script takes one to this directory in cmd.exe. %SystemDrive%\Users · HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders ·...