Each new shell
PowerShell Profiles: Persistence in profile.ps1
How PowerShell profile scripts run in every new session, which of the profile paths attackers change, and how to find, read and clean a modified profile.
- MITRE ATT&CK
- T1546.013
- Tactics
- Persistence, Privilege escalation
- Runs
- Each new shell
- Set up by
- Users and administrators
- Locations
- C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
- C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
- C:\Program Files\PowerShell\7\profile.ps1
- %USERPROFILE%\Documents\WindowsPowerShell\profile.ps1
- %USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
- %USERPROFILE%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
How it works
A PowerShell profile is a script that PowerShell runs at the start of every session, in the session's own scope, before the first prompt. Anything it defines (functions, aliases, variables) stays available; anything it starts runs as the account that opened PowerShell. There are four profiles, run in this order:
| Profile | Windows PowerShell 5.1 | PowerShell 7 |
|---|---|---|
| All users, all hosts | C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 | C:\Program Files\PowerShell\7\profile.ps1 |
| All users, current host | …\v1.0\Microsoft.PowerShell_profile.ps1 | …\7\Microsoft.PowerShell_profile.ps1 |
| Current user, all hosts | Documents\WindowsPowerShell\profile.ps1 | Documents\PowerShell\profile.ps1 |
| Current user, current host | Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 | Documents\PowerShell\Microsoft.PowerShell_profile.ps1 |
The all-users folder is $PSHOME; 32-bit Windows PowerShell uses C:\Windows\SysWOW64\WindowsPowerShell\v1.0. The per-user files are in the user's Documents folder, which follows folder redirection and OneDrive's known folder move (OneDrive\Documents). "Current host" files are per program: the console uses Microsoft.PowerShell_profile.ps1, the ISE Microsoft.PowerShellISE_profile.ps1, and VS Code's extension Microsoft.VSCode_profile.ps1. $PROFILE holds the four paths for the running host.
Profiles load for interactive windows and also for powershell.exe -Command or -File, which is how scripts, scheduled tasks and management agents usually start PowerShell. Only -NoProfile skips them. Remote sessions through WinRM do not run profiles.
Profiles are scripts, so the execution policy applies. Windows client editions default to Restricted, where no profile runs; Windows Server and PowerShell 7 default to RemoteSigned, which runs local files. Many organisations set RemoteSigned or Bypass by Group Policy, and a user can set the CurrentUser scope without administrator rights. On any machine where scripts run, the profile runs.
Where it lives
| Location | Default | Who can write it |
|---|---|---|
C:\Windows\System32\WindowsPowerShell\v1.0\*profile.ps1 (and SysWOW64) | Absent | Administrators |
C:\Program Files\PowerShell\7\*profile.ps1 | Absent | Administrators |
C:\Users\<name>\Documents\WindowsPowerShell\*profile.ps1 | Absent | The user |
C:\Users\<name>\Documents\PowerShell\*profile.ps1 | Absent | The user |
C:\Windows\System32\config\systemprofile\Documents\WindowsPowerShell\*profile.ps1 | Absent | Administrators; it is SYSTEM's own per-user profile |
No profile exists on a stock Windows 10 or 11 system. Developers and administrators create their own, and some tools write to them: conda init adds a #region conda initialize block, Chocolatey adds an import of its profile module, and prompt themes add an initialisation line. Those are what a normal profile looks like.
How attackers use it
Appending a line to a profile is quiet persistence that needs no registry change, no service and no task; cmd.exe's AutoRun value is the equivalent for the command prompt:
Add-Content -Path $PROFILE -Value 'Start-Process -WindowStyle Hidden C:\Users\alice\AppData\Local\Temp\upd.exe'
With administrator rights, the all-users profile reaches every account, including administrators' consoles and SYSTEM processes that start PowerShell without -NoProfile, such as management agents and scheduled tasks. That makes it a privilege escalation path as well: code an administrator plants once runs later as SYSTEM.
Add-Content -Path $PROFILE.AllUsersAllHosts -Value '. C:\ProgramData\Microsoft\Diagnosis\diag.ps1'
Turla used profile changes to relaunch its PowerShell loaders, and ATT&CK lists the group for the technique. Common disguises:
- a line hidden at the end of a long legitimate profile, or after many blank lines so it is off-screen in an editor;
- code inside a block that imitates the conda or Chocolatey sections;
- a dot-sourced script or an imported module stored elsewhere, so the profile itself holds one harmless-looking line;
-EncodedCommand,FromBase64Stringor a download cradle (IEX (New-Object Net.WebClient).DownloadString(…));- work started as a background job or hidden process, so the prompt appears as fast as usual.
Because profiles run in the user's session, they are also used to wrap commands: a function named like Get-Credential or Connect-AzAccount that records what an administrator types.
How to check a host
On a live machine, list every profile file, including other users':
$PROFILE | Select-Object *
Get-ChildItem -Force -ErrorAction SilentlyContinue -Path C:\Windows\System32\WindowsPowerShell\v1.0\*profile.ps1, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\*profile.ps1, 'C:\Program Files\PowerShell\*\*profile.ps1', C:\Users\*\Documents\*PowerShell\*profile.ps1, C:\Users\*\OneDrive*\Documents\*PowerShell\*profile.ps1, C:\Windows\System32\config\systemprofile\Documents\*PowerShell\*profile.ps1
Get-ExecutionPolicy -List
Open each file in an editor that shows its full length, not with Get-Content | Select-Object -First. Check what it dot-sources or imports, and Get-AuthenticodeSignature if the organisation signs its scripts.
Offline, collect the same paths and read them; the file's NTFS times (in the $MFT, both $STANDARD_INFORMATION and $FILE_NAME) date the change. With script block logging on, event 4104 in Microsoft-Windows-PowerShell/Operational records the profile's code each time it runs; Windows PowerShell 5.1 also logs script blocks it considers suspicious as warnings without any configuration. With module logging on, event 4103 records the commands it ran. A user's PSReadLine history (%APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt) may show the Add-Content or notepad $PROFILE that changed it.
Suspicious: any all-users profile on a machine where nobody manages one; network calls, encoded commands, hidden processes or Add-MpPreference in a profile; dot-sourced files in ProgramData, Temp or Public; a profile modified at the time of an intrusion.
What Windows Persistence flags
The tool reads profile files found in the collection: the all-users profiles in C:\Windows\System32\WindowsPowerShell\v1.0 and SysWOW64 (profile.ps1, Microsoft.PowerShell_profile.ps1, Microsoft.PowerShellISE_profile.ps1) and in any folder under C:\Program Files\PowerShell (7, 7-preview…); SYSTEM's own profile under C:\Windows\System32\config\systemprofile\Documents\WindowsPowerShell or \PowerShell, shown with the user SYSTEM; and per-user ones under Documents\WindowsPowerShell or Documents\PowerShell, including a OneDrive Documents folder (the VS Code profile too). The collection command copies all of these.
Comments (# lines and <# … #> blocks) are removed first; a file with no code left is skipped. Otherwise it becomes one entry: its command shows the code lines joined (first 300 characters), the details show the first five lines and the line count, and the time is the file's modification time.
An all-users profile raises PowerShell profile for every user (low). The rules then run on the first 256 KB of the code, read as a script body, so only what the text does counts: Downloads and runs code, Encoded PowerShell command (with the decoded text), Obfuscated command, Runs with a hidden window, Bypasses the PowerShell execution policy, Changes Defender settings, Deletes backups or recovery, Creates an account or grants admin, Clears event logs, Runs from a temporary or public folder and Runs from a network share. The rules about a program's own path and name, such as Windows program name in the wrong place or Machine-made name, do not apply. Written date marks a profile modified in the 7 days before the newest time in the collection. All-users profiles are filed as Windows locations, but they always carry a flag, so the "Hide Windows entries" filter hides no profile.
Limits: the rules do not follow dot-sourced scripts or imported modules. A profile entry has no program path, so the Defender exclusion and same-program cross-checks do not apply. The tool does not read the execution policy, so it cannot say whether the profile actually runs.
Cleaning up
- Preserve: copy the profile with its times (
robocopy <folder> C:\IR\ps <file> /COPY:DAT /DCOPY:T), hash it withGet-FileHash, and save any script it dot-sources. - Remove the attacker's lines, or the whole file if the user never had a profile. Keep the user's own lines; ask before deleting a profile someone relies on.
- Remove what the profile pointed to: dot-sourced scripts, modules dropped in a
PSModulePathfolder, the payload it started. - Reset an execution policy the attacker loosened:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Undefined.
Then check every profile path on the host, not only the one found: attackers often write several. Anything typed in a PowerShell session while the profile was active could have been recorded, so rotate the credentials administrators used there, including cloud and API tokens.
See also
Related techniques
On Windows Forensics
The artifacts behind this technique, in the Windows Forensics reference: