On Windows 7:
[PS]> echo $ENV:UserProfile
C:\Users\arco444
This returns the path to the profile directory. Therefore I'd expect looking only for the username to fail the condition. I'd do a simple match instead:
if ($env:userprofile -imatch "rmullins")
{
Remove-Item $env:userprofile\Desktop\ITFILES -Recurse -Force
}
Answer from arco444 on Stack OverflowHow do I pull the logged in userprofile and not the running profile?
${Env:USERPROFILE} in administrator shell
Variable for user profile paths
Get userprofile path for current logged in user.
Videos
I am working on a script to install Epicor.exe through PowerShell. I am using $desktopPath = "$($env:userprofile)\Desktop" for part of this script but instead of going to the logged on user's desktop it is going to the user desktop of the person running the script. I.E. If an admin runs this, it will go to the admins desktop.
$env:username will take the username of whatever account is running the script, it will not get the logged in user.
Maybe put it something to make sure the actual temp folder doesn’t get deleted
Get-ChildItem c:\users\*\appdata\local\temp -Recurse | remove-item -force
I need to write a script that, in addition to other things, will clean out temp files underneath user profiles, the path would be:
c:\users\USERNAME\appdata\local\temp
Where USERNAME should be any and all users in that folder. I’m not sure how to accomplish this, scripting is definitely my weakness.
TIA!
As @FrankMerrow posted, on this Stack Overflow question you will find the answer, but the correct is the one from Neck Beard, I'll copy it here.
As @woter324 points out issuing $profile | select * will show you the paths from where PowerShell gets profiles. As stated in the MS documentation:
The profiles are listed in precedence order. The first profile has the highest precedence.
<username>
profile | select *
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\<username>\Documentos\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\<username>\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length : <will vary>
Those above are mine's, I supose you get something like
CurrentUserAllHosts : H:\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : H:\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
In order to edit those paths you have two ways
Via regedit
Edit the key with name Personal that you will find under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
Via PowerShell
I didn't test this one, in the linked thread a user says it has to be tweaked in order to work but I supose it should be easy to get through.
Issue
New-ItemProperty
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
Personal -Value 'Your New Path Here' -Type ExpandString -Force
Either way you have to reboot Power Shell (or windoes terminal) for this to take effect. You can then check again with $profile | select *.
You might also check out this post on Stack Overflow. The best solution offered so far (to my almost identical question) is to change $profile.AllUsersAllHosts to "dot source" another file of your own choosing.
I've seen nothing so far to indicate you can change the default value of $profile itself.
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.
The %UserProfile% variable is a special system-wide environment variable that is complete in and of itself.
It contains %SystemDrive%\Users\{username}
See this fantastic table that highlights the differences between variables in windows XP (NT5) and Windows Vista/7/8 (NT6).
Try
mkdir %userprofile%\AppData\Roaming\modinstaller\mods
Its value is the location of the current user's profile directory, in which is found that user's HKEY_CURRENT_USER (HKCU) registry hive (NTUSER).
I assume you mixed up the variables %USERPROFILE% and %USERNAME%.
By default, %USERPROFILE% and C:\Users\%USERNAME% point to the same location. Since this is not guaranteed to be true, using %USERPROFILE% is a more reliable approach.
In general, when debugging a command like
mkdir C:\Users\%USERPROFILE%\AppData\Roaming\modinstaller\recovery
your first step should be to prepend echo.
The command
echo mkdir C:\Users\%USERPROFILE%\AppData\Roaming\modinstaller\recovery
would have shown you the following:
mkdir C:\Users\C:\Users\Tristan\AppData\Roaming\modinstaller\recovery
which is clearly not what you want.
You can also query the value of %USERPROFILE% by executing
set USERPROFILE
To see all currently defined environment variables, execute
set
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: