Repository navigation
Why does profile creation fail? #28304
Relevant Context
…and
What The Problem IsHowever, when I invoke
What My Environment Is
|
Replies: 2 comments 2 replies
|
You can confirm this with If you don't feel comfortable with recommending if (-not Test-Path $profile) { New-Item $Profile -Type File -Force } else { Write-Warning 'Existing Profile found - Skipping' } |
|
The error occurs because the parent directory of On Linux, PowerShell typically stores the current user's profile under You can safely create the directory first, then create the profile file only if it doesn't already exist: $profileDirectory = Split-Path -Parent $PROFILE
if (-not (Test-Path -LiteralPath $profileDirectory)) {
New-Item -Path $profileDirectory -ItemType Directory -Force
}
if (-not (Test-Path -LiteralPath $PROFILE)) {
New-Item -Path $PROFILE -ItemType File
}This avoids using You can verify the result with: Test-Path -LiteralPath $PROFILEThe command should return |
New-Item $Profile -Type Filewill fail on systems where PowerShell hasn't already been installed because the folderpowershellthat will contain the profile script, doesn't yet exist & is why you are gettingSystem.IO.DirectoryNotFoundException: Could not find a part of the path '/home/RokeJulianLockhart/.config/powershell/Microsoft.PowerShell_profile.ps1You can confirm this with
Test-Path $PROFILE- FALSE - this tests the full path to the profileTest-Path $PROFILE\..\- FALSE - this tests the parent directory, which hasn't been createdtest-path $PROFILE\..\..- TRUE - this tests the grandparent directory, which should exist.If you don't feel comfortable with recommending
New-Item $Pr…