PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
44.75k stars 7.24k forks source link

Error when creating powershell profile using documentation command: "Could not find a part of the path" #23995

Open tommyhosman opened 3 months ago

tommyhosman commented 3 months ago

Prerequisites

Steps to reproduce

Goal Create profile as described by the documentation

Original issue with PS 5.x.x. But problem persists with PS 7.4.3.

Steps

  1. No previous .ps1 file exists. And no folders to the default $profile path are present on the machine.
  2. Open powershell as administrator.
  3. Run New-Item -Path $profile -ItemType "file" -Force

Expected behavior

Success statement. And created .ps1 file.

Actual behavior

PowerShell 7.4.3
PS C:\Users\Tommy> New-Item -Path $profile -ItemType "file" -Force
New-Item: Could not find a part of the path 'C:\Users\Tommy\Documents\PowerShell\Microsoft.PowerShell_profile.ps1'.

### Error details

```console
PS C:\Users\Tommy> Get-Error

Exception             :
    Type       : System.IO.DirectoryNotFoundException
    TargetSite :
        Name          : CreateFile
        DeclaringType : [Microsoft.Win32.SafeHandles.SafeFileHandle]
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : Could not find a part of the path
'C:\Users\Tommy\Documents\PowerShell\Microsoft.PowerShell_profile.ps1'.
    Source     : System.Private.CoreLib
    HResult    : -2147024893
    StackTrace :
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access,
FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare
share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share,
FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access,
FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Microsoft.PowerShell.Commands.FileSystemProvider.NewItem(String path, String type, Object value)
TargetObject          : C:\Users\Tommy\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
CategoryInfo          : WriteError: (C:\Users\Tommy\Docu…erShell_profile.ps1:String) [New-Item],
DirectoryNotFoundException
FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand
InvocationInfo        :
    MyCommand        : New-Item
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 1
    Line             : New-Item -Path $profile -ItemType "file" -Force
    Statement        : New-Item -Path $profile -ItemType "file" -Force
    PositionMessage  : At line:1 char:1
                       + New-Item -Path $profile -ItemType "file" -Force
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : New-Item
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
      0
      1

### Environment data

```powershell
Name                           Value
----                           -----
PSVersion                      7.4.3
PSEdition                      Core
GitCommitId                    7.4.3
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


### Visuals

_No response_
MartinGC94 commented 3 months ago

That's odd. It works perfectly fine on my PC, even if I delete the PowerShell folder. Does it work if you manually create the folder? How about some other path than $profile like C:\NewFolderTest\demo.ps1?

SteveL-MSFT commented 1 month ago

So the FileSystemProvider when -Force is passed will explicitly try to create all of the intermediate folders. What I wonder here is if your My Documents is one OneDrive and the folder creation only exists in OneDrive and not on the local filesystem which would explain the error?