I am able to create a symbolic link using mklink.exe (MKLINK /D MYDIR c:\TEMP\MYDIR)
I want to do the same thing in powershell.
Technology Tips and News
I am able to create a symbolic link using mklink.exe (MKLINK /D MYDIR c:\TEMP\MYDIR)
I want to do the same thing in powershell.
I tried this
$s = new-pssession -computername mymachine
enter-pssession $s
cmd /c mklink /d e:\DPM_SQL_TEMP C:\Temp\DPM_SQL_TEMP
exit-pssession
remove-pssession $s
Got the error
cmd.exe : 'mklink' is not recognized as an internal or external command,
At C:\Users\user\Documents\MountPoint.ps1:13 char:5
+ cmd <<<< /c c:\windows\system32\mklink /d e:\DPM_SQL_TEMP C:\Temp\DPM_SQL_TEMP
+ CategoryInfo : NotSpecified: ('c:\windows\sys...ternal command,:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
operable program or batch file.
Hi,
Try this script:
$target = "c:\test" $path = "c:\test2.lnk" $wshshell = New-Object -ComObject WScript.Shell $link = $wshshell.CreateShortcut($path) $link.TargetPath = $target $link.Save()
http://stackoverflow.com/questions/894430/powershell-hard-and-soft-links
PowerShell Community Extensions
There are several cmdlets for reparse points of various types: New-HardLink, New-SymLink, New-Junction, Remove-ReparsePoint and others.
use this:I tried this
$s = new-pssession -computername mymachine
enter-pssession $s
cmd /c mklink /d e:\DPM_SQL_TEMP C:\Temp\DPM_SQL_TEMP
exit-pssession
remove-pssession $s
Tried this
& cmd /c "mklink" /d "e:\DPM_SQL_TEMP" "C:\Temp\DPM_SQL_TEMP"
Got the error
cmd.exe : The system cannot find the path specified.
At C:\Users\user\Documents\MountPoint.ps1:13 char:6
+ & <<<< cmd /c "mklink" /d "e:\DPM_SQL_TEMP" "C:\Temp\DPM_SQL_TEMP"
+ CategoryInfo : NotSpecified: (The system cann...path specified.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
yeah you somehow remove the escape tics -> `
you have one " at the beginning and one " at the end. inbetween you have to escape \ and " with an `
so it looks like: `\ and `"
Then just use Invoke-Expression:
Invoke-Expression -Command "cmd /c mklink /d e:\DPM_SQL_TEMP C:\Temp\DPM_SQL_TEMP"
yeah you somehow remove the escape tics -> `
you have one " at the beginning and one " at the end. inbetween you have to escape \ and " with an `
so it looks like: `\ and `"
& cmd /c "mklink `/j `"e:`\DPM_SQL_TEMP`" `"E:`\temp`\DPM_SQL_TEMP`\`""
It doesn't work!
$s = new-pssession -computername mymachine
enter-pssession $s
& cmd /c "mklink `/j `"e:`\DPM_SQL_TEMP`" `"E:`\temp`\DPM_SQL_TEMP`\`""
exit-pssession
remove-pssession $s
Error
cmd.exe : Local volumes are required to complete the operation.
At C:\Users\srabhi_adm\Documents\MountPoint.ps1:13 char:6
+ & <<<< cmd /c "mklink `/j `"e:`\DPM_SQL_TEMP`" `"E:`\temp`\DPM_SQL_TEMP`\`""
+ CategoryInfo : NotSpecified: (Local volumes a... the operation.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
If I remote login into mymachine and then do a mklink I can successfully create the link in E drive which is a local disk/volume.
I am able to create a symbolic link using mklink.exe (MKLINK /D MYDIR c:\TEMP\MYDIR)
I want to do the same thing in powershell.
$signature = @" using System; using System.Runtime.InteropServices; namespace System { public class MkLink { [DllImport("kernel32.dll")] public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); } } "@ Add-Type -TypeDefinition $Signature [system.MkLink]::CreateSymbolicLink("mydir","C:\tempMydir",1)
Ps.Must have administrator privileges.
I am able to create a symbolic link using mklink.exe (MKLINK /D MYDIR c:\TEMP\MYDIR)
I want to do the same thing in powershell.
$signature = @" using System; using System.Runtime.InteropServices; namespace System { public class MkLink { [DllImport("kernel32.dll")] public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); } } "@ Add-Type -TypeDefinition $Signature [system.MkLink]::CreateSymbolicLink("mydir","C:\tempMydir",1)
Ps.Must have administrator privileges.
Try this...
$s = new-pssession -computername mymachine
invoke-command -session $s -scriptblock {cd c:\test; start-process 'c:\windows\system32\cmd.exe' -argumentlist '/C mklink /D MYTEST c:\TEMP\MYTEST'}
$s | remove-pssession
Make sure you change directory to the folder where you want the link to appear before executing.
Only works on Vista & above, mklink is not available in previous versions.
BTW mklink.exe does not exist.. it's an internal command in cmd.exe, like copy, set, sta
Hi,
You can create a new symbolic link or junction in Windows Vista or Windows Server® 2008 with New-Symlink and New-Junction, respectively. To use this cmdlets, please install the PowerShell Community Extensions firstly.
Meanwhile, you could try the following scripts:
MKLINK – Make Symbolic File and Directory links. Useful for making symbolic links from powershell.
Best Regards
Dale
I finally got this to work. I could not install Powershell community extensions even after hours of debugging. for the second mklink code, I build the DLL file but then I didn't know how to use it from Powershell.
This is the code which finally worked for me.
cls $test1 = test-path -path '\\mymachine\e$\DPM_SQL_PROTECT' if ($test1 -eq $false) { $s = new-pssession -computername mymachine <br/> enter-pssession $s Invoke-Command -Session $s -ScriptBlock {set-location "e:\"} Invoke-Command -Session $s -ScriptBlock {& cmd /c "mklink /d DPM_SQL_PROTECT c:\temp\DPM_SQL_PROTECT\"} exit-pssession remove-pssession $s }