How to create a symbolic link using PowerShell?

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.

March 16th, 2011 8:25am

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.

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 9:22am

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()
March 16th, 2011 9:31am

I don't think shortcut and a link to a directory are the same thing.... feel free to correct me if I am wrong.
Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 9:40am

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.

March 16th, 2011 9:48am

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

 

use this:
& cmd /c "mklink `/j `"e:`\DPM_SQL_TEMP`" `"E:`\temp`\DPM_SQL_TEMP`\`""
Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 9:52am

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

March 16th, 2011 10:12am

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 `"

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 10:14am

Then just use Invoke-Expression:

Invoke-Expression -Command "cmd /c mklink /d e:\DPM_SQL_TEMP C:\Temp\DPM_SQL_TEMP"

March 16th, 2011 10:22am

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`\`""

http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 10:28am

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.

March 16th, 2011 10:55am

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.

  • Proposed as answer by StevenNS 11 hours 47 minutes ago
Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 1:33pm

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.

  • Proposed as answer by StevenNS Wednesday, July 01, 2015 7:21 PM
March 16th, 2011 1:33pm

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

Free Windows Admin Tool Kit Click here and download it now
March 17th, 2011 3:09am

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.

http://poshcode.org/2353

 

Best Regards

Dale

March 17th, 2011 5:52am

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
}

  • Marked as answer by MSDN Student Thursday, March 17, 2011 12:29 PM
Free Windows Admin Tool Kit Click here and download it now
March 17th, 2011 12:28pm

Great fix. Works perfect.
July 1st, 2015 3:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics