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 Overflow
🌐
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
Discussions

How do I pull the logged in userprofile and not the running profile?
Not in front of a computer but something close to this run as administrator iirc. Get-Item "C:\Users\*\Desktop\Name Of Shortcut.lnk" | Remove-Item More on reddit.com
🌐 r/PowerShell
26
26
March 25, 2023
${Env:USERPROFILE} in administrator shell
Hello Powershell users. I have a script that will be running on local accounts on different machines, but the script itself needs admin privileges to run properly. The problem is that I need to use environment variables like ${USERPROFILE}, so the path will be correct on the current logged in user. More on forums.powershell.org
🌐 forums.powershell.org
5
0
February 4, 2016
Variable for user profile paths
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 ... More on community.spiceworks.com
🌐 community.spiceworks.com
7
6
May 14, 2019
Get userprofile path for current logged in user.
If $env:USERPROFILE doesn't work, is this because the software you're using is running using either SYSTEM or a service account? Also, give this a read . Enumerating the HKEY_USERS registry hive might be the way to go. More on reddit.com
🌐 r/PowerShell
18
May 28, 2021
🌐
Reddit
reddit.com › r/powershell › how do i pull the logged in userprofile and not the running profile?
r/PowerShell on Reddit: How do I pull the logged in userprofile and not the running profile?
March 25, 2023 -

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.

🌐
PowerShell Forums
forums.powershell.org › powershell help
${Env:USERPROFILE} in administrator shell - PowerShell Help - PowerShell Forums
February 4, 2016 - Hello Powershell users. I have a script that will be running on local accounts on different machines, but the script itself needs admin privileges to run properly. The problem is that I need to use environment variables like ${USERPROFILE}, so the path will be correct on the current logged in user.
🌐
Rene Nyffenegger
renenyffenegger.ch › notes › Windows › dirs › Users › username › index
%USERPROFILE%
Apparently, in Windows 10, the variable %CSIDL_PROFILE% is equivalently used for %USERPROFILE%. ... The junctions' (aka reparse points) name and target can be shown in cmd.exe with dir /al. %USERPROFILE% also stores the registry hive for the current user (HKEY_CURRENT_USER) in the file %USERPROFILE%\NTUSER.DAT. In PowerShell, the home directory of a user is stored in the automatic variable $home.
Find elsewhere
Top answer
1 of 6
16

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 *.

2 of 6
10

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.

🌐
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.

🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_profiles
about_Profiles - PowerShell | Microsoft Learn
September 29, 2025 - You can use the $PROFILE variable in many commands. For example, the following command opens the "Current User, Current Host" profile in Notepad: ... The following command determines whether an "All Users, All Hosts" profile has been created on the local computer: ... For example, to create a profile for the current user in the current PowerShell host application, use the following command:
🌐
Netwrix
netwrix.com › en › resources › blog › powershell-environment-variables
PowerShell Environment Variables
August 25, 2025 - To list all the environment variables available in your PowerShell session, simply execute the following command. ... The output will typically include a wide array of environment variables, some of which you may recognize. PATH: A list of directories where executable files are stored. TEMP: The path to the temporary file storage directory. USERPROFILE: The path to the current user’s profile directory e.g., C:\Users\Administrator.Milkyway.
🌐
TechTarget
techtarget.com › searchwindowsserver › tutorial › How-to-find-and-customize-your-PowerShell-profile
How to find and customize your PowerShell profile | TechTarget
The easiest way to access PowerShell's profile is through PowerShell itself. There's no need to remember any of the listed paths because they are all stored in a variable called $Profile.
Published   September 16, 2025
🌐
Powershell Commands
powershellcommands.com › powershell-userprofile
Mastering the PowerShell UserProfile: A Quick Guide
May 27, 2024 - The `UserProfile` in PowerShell refers to the environment variable that points to the current user's profile folder, allowing users to access personal files and settings easily.
🌐
LazyAdmin
lazyadmin.nl › home › how to create a powershell profile – step-by-step
How to Create a PowerShell Profile - Step-by-Step — LazyAdmin
May 17, 2024 - To find your PowerShell Profile location we are going to use PowerShell. ... The $profile variable returns the profile for the current user in the current program (host). But as mentioned, there are different profiles.
🌐
GitHub
github.com › PowerShell › PowerShell › issues › 8069
Move PowerShell folder on Windows from ${env:USERPROFILE}\OneDrive\Documents\PowerShell to ${env:USERPROFILE}\.powershell · Issue #8069 · PowerShell/PowerShell
October 18, 2018 - To correct this, and provide one less reason for users' free OneDrive space to be completely and unnecessarily consumed, I propose that the Documents\PowerShell folder for PowerShell Core on Windows be moved and renamed to ${env:USERPROFILE}\.powershell.
Author   KirkMunro
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › learn › shell › creating-profiles
Customizing your shell environment - PowerShell | Microsoft Learn
November 21, 2025 - The location varies depending on the operating system and the version of PowerShell you use. By default, referencing the $PROFILE variable returns the path to the "Current User, Current Host" profile.
🌐
Adam the Automator
adamtheautomator.com › powershell-environment-variables
PowerShell Environment Variables: A Deep Dive
The REG utility allows us to see the native value of the registry value. The value type is REG_EXPAND_SZ and the value contains the %USERPROFILE% environment variable. If you’d rather use PowerShell to retrieve the registry value, you can so using the Get-Item cmdlet as shown below.
Published   October 1, 2023
Top answer
1 of 4
1
$ErrorActionPreference = "SilentlyContinue"$Path = "C:\Users"$UserFolders = Get-ChildItem -Path $Path -Directory$currentDate = Get-Date$ageLimit = 60# Set the execution policy to bypass (use with caution in production)Set-ExecutionPolicy Bypass -Force# Loop through each user profile folderForEach ($UserFolder in $UserFolders) {    $UserName = $UserFolder.Name    # Only process profiles that have an NTUser.dat file    $NTUserPath = "$Path\$UserName\NTUSER.DAT"    If (Test-Path $NTUserPath) {        $Dat = Get-Item $NTUserPath -Force        $DatTime = $Dat.LastWriteTime        # Reset NTUSER.DAT's LastWriteTime to match the folder's LastWriteTime        If ($UserFolder.Name -ne "default") {            $Dat.LastWriteTime = $UserFolder.LastWriteTime        }    }    # Calculate profile age and delete if older than $ageLimit    $profileAge = ($currentDate - $UserFolder.LastWriteTime).Days    If ($profileAge -ge $ageLimit) {        # Remove user profile and any content inside it (use with caution)        Try {            Remove-Item -Path $UserFolder.FullName -Recurse -Force            Write-Host "Deleted profile: $UserName"        } Catch {            Write-Host "Error deleting profile: $UserName - $_"        }    }}# Optional: Clean up empty folders (if necessary)$EmptyFolders = Get-ChildItem -Path $Path -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }ForEach ($EmptyFolder in $EmptyFolders) {    Try {        Remove-Item -Path $EmptyFolder.FullName -Force        Write-Host "Deleted empty folder: $($EmptyFolder.Name)"    } Catch {        Write-Host "Error deleting empty folder: $($EmptyFolder.Name) - $_"    }}Write-Host "Profile cleanup complete."
2 of 4
0
Your script is on the right track for deleting old user profiles, but there are a few improvements that can make it more efficient, avoid some pitfalls (such as leaving empty folders), and potentially clean up obsolete or unnecessary parts of the code. Here's an enhanced version of the script, with some optimizations and added checks:Suggestions & Improvements:Use -Directory when getting user profiles: You can directly filter for directories with -Directory in the Get-ChildItem cmdlet, reducing the need for additional checks.Ensure NTUser.dat exists: You don't need to call Get-Item multiple times in a loop if you can check for existence first. I’ve added that check with Test-Path.Handle empty profile folders: When a profile folder is removed, we should check if it’s empty before attempting to remove it. If it's empty, you can remove it with Remove-Item as well.Use -Force for visibility of hidden files: This ensures hidden files like NTUser.dat are also handled properly.Remove the report object: If you're not using $Report, it can be omitted.Ensure execution policy is set before script runs: It’s more logical to set the execution policy at the start, so it’s enforced before running any other script actions.