2021年3月12日星期五

Symlinks cannot be created in Powershell 5.1 but can be created by Powershell 7 and Command Prompt

Windows Developer Mode is enabled, all of the following are non admin shells. In all of the scenarios detailed below, a file named target exists in the current directory.

Via Powershell 5.1

PS C:\Users\user\repos\tmp> Get-Host | Select-Object Version  Version  5.1.19041.610  PS C:\Users\user\repos\tmp> New-Item -Path link -ItemType SymbolicLink -Target target  New-Item : Administrator privilege required for this operation.  At line:1 char:1  + New-Item -Path link -ItemType SymbolicLink -Target target  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      + CategoryInfo          : PermissionDenied: (C:\Users\user\repos\tmp\target:String) [New-Item], UnauthorizedAccessException      + FullyQualifiedErrorId : NewItemSymbolicLinkElevationRequired,Microsoft.PowerShell.Commands.NewItemCommand  

Via Powershell 7.1

PS C:\Users\user\repos\tmp> Get-Host | Select-Object Version  Version  7.0.2  PS C:\Users\user\repos\tmp> New-Item -Path link -ItemType SymbolicLink -Target target  la---           3/13/2021  1:55 AM              0 link -> target  

Via both Powershell 5.1 and Powershell 7 the following works

PS C:\Users\user\repos\tmp> cmd /c mklink link .\target  symbolic link created for link <<===>> .\target  

Via Command Prompt the following works

C:\Users\user\repos\tmp>mklink link target  symbolic link created for link <<===>> target    

Does anyone have an idea why only Powershell 5.1 is giving this error, and is there a way to solve this?

https://stackoverflow.com/questions/66609154/symlinks-cannot-be-created-in-powershell-5-1-but-can-be-created-by-powershell-7 March 13, 2021 at 09:05AM

没有评论:

发表评论