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 OverflowHello 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.
Powershell script to get current user path using local system credentials
Powershell filepath for current user's documents folder?
powershell get current users path variable
How to get current user's AppData folder within a script ran as system context
Videos
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.
I tried searching via reged also but can’t make comparison with current user.
Can anybody help me
you can do something like
$users=gci c:\users | where name -NotMatch 'Public|default'
foreach($user in $users){
gci "
user.fullname)\AppData\Local\Temp"|Remove-Item -Force -Recurse
}
or
$user=(Get-WmiObject -Class win32_computersystem).UserName.split('\')[1]
"c:\users\$user"
If you're looking to do it in PowerShell, assuming you have the ActiveDirectory Module loaded (ipmo ActiveDirectory) you can do
Get-ADUser ($env:UserName)
The logon Username should be unique on the domain and Get-ADUser will use the current domain by default
After poking around for a while, I stumbled upon this little beauty, from the .NET API:
public static UserPrincipal Current { get; }
This has been supported since .NET 3.5, so it's perfect for older Windows OSes as well. In PowerShell, it can be used like so:
# Load in the Assembly for this stuff
[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement") | Out-Null
Function Get-CurrentUser {
[System.DirectoryServices.AccountManagement.UserPrincipal]::Current
}
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 Userany advice how can I complete this?
Thanks.
In powershell, use the Win32_UserProfile WMI Object to find profiles remotely:
gwmi -ComputerName <computername> Win32_UserProfile
To find user profiles not on a server (or that are, either way), you could do something like:
gwmi -ComputerName <computername> Win32_UserProfile | ? {"C:\Users\<username>" -contains $_.LocalPath}
If the path exists, it will give results if not then it won't. You can do fancier stuff than this, but basically this should accomplish what you need without using regular expressions.
For current user run:
$env:USERPROFILE
To get list of all environmental variables, run:
Get-ChildItem Env:
If you open PS as another user, $env:USERPROFILE becomes that user. How can I find the interactive user that's actually logged in to Windows and its home path?
Thanks
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.
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
}