Creating relative symbolic links in powershell 5.1 is not so easy. New-Item does not work as expected. Listed below are some approaches. Did I miss something?
Setting example for all examples:
mkdir C:\Temp\foo -ErrorAction SilentlyContinue 'sample contents' > C:\Temp\foo\foo.txt cd C:\Temp
Example1: does not work
#new ps5 Item cmdlets (https:
Sample 2: does not work
#Powershell community extensions
Sample 3: works as expected
#API call CreateSymbolicLink as per https://gallery.technet.microsoft.com/scriptcenter/new-symlink-60d2531e #.\foo and .\foo\foo.txt are returned Add-Type -MemberDefinition @' [DllImport("kernel32.dll", EntryPoint = "CreateSymbolicLinkW", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); public static DirectoryInfo CreateSymbolicLinkToFolder(string lpSymlinkFileName, string lpTargetFileName) { bool res = CreateSymbolicLink(lpSymlinkFileName, lpTargetFileName, 1); if (!res) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return (new DirectoryInfo(lpSymlinkFileName)); } public static FileInfo CreateSymbolicLinkToFile(string lpSymlinkFileName, string lpTargetFileName) { bool res = CreateSymbolicLink(lpSymlinkFileName, lpTargetFileName, 0); if (!res) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return (new FileInfo(lpSymlinkFileName)); } '@ -Name Win32 -NameSpace System -UsingNamespace System.ComponentModel, System.IO [Win32]::CreateSymbolicLinkToFolder("c:\Temp\bar", ".\foo") [Win32]::CreateSymbolicLinkToFile("c:\Temp\bar.txt", ".\foo\foo.txt")
Sample 4: works as expected
#using mklink from cmd produces correct relative paths #.\foo and .\foo\foo.txt are returned cmd /c mklink /d "c:\Temp\bar" ".\foo" cmd /c mklink "c:\Temp\bar.txt" ".\foo\foo.txt" (Get-Item "c:\Temp\bar").Target (Get-Item "c:\Temp\bar.txt").Target
Edit: Sample3 has been updated to write in unicode api and GetLastError
source share