How to create relative symlink powershell way? How to create relative symlink powershell way? powershell powershell

How to create relative symlink powershell way?


Starting with pwsh 7.1.X sample1 works as expected:

#new ps5 Item cmdlets (https://msdn.microsoft.com/en-us/powershell/wmf/5.0/feedback_symbolic) are not working well with relative paths#C:\Temp\foo and C:\Temp\foo\foo.txt are returned$fld = New-Item -ItemType SymbolicLink -Name 'bar' -Target '.\foo'$fl = New-Item -ItemType SymbolicLink -Name 'bar.txt' -Target '.\foo\foo.txt'$fld.Target$fl.Target


You might try adding this function to your $PROFILE.

Function SymbolicLink-Add {[CmdLetBinding( )]Param ( [string] $File, [string] $SymbolicLinkFolder )$LinkFile = (Get-Item $File)if ( $false -eq $LinkFile.Exists ){    return "Cannot create symbolic link file does not exist: [$File]"}if ( $false -eq ( Test-Path $SymbolicLinkFolder ) ){    New-Item -Type Directory -Path $SymbolicLinkFolder     -ErrorAction Stop}New-Item -ItemType SymbolicLink -Path $SymbolicLinkFolder -Name $LinkFile.Name -Value $LinkFile}


Try something like this:

$fld = [string]"C:\Temp\bar"$fl = [string]"C:\Temp\foo\foo.txt"# Link to FileNew-Item -ItemType SymbolicLink -Target $fl -Path $fld  -Force# Link to DirectoryNew-Item -ItemType Junction -Path C:\Temp\bar -Target C:\Temp\foo -Force