0

I'm trying to create shortcut in Windows 7, but after I try and a few google, turn out there is no way in PowerShell.

Question: Is there any other way to batch create shortcut that support unicode path and filename?

Tried using symbolic link, but it's not working (returns file not exist) when a unicode path.

New-Item -ItemType SymbolicLink -Path "C:\f[o]o♭\pr[o]file♭.txt" -Value "C:\b[a]r♭\pr[o]file♭.txt"

Code I used, but didn't work when unicode path, filename.

$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($lnkpath)
$Shortcut.TargetPath = $tarpath
$Shortcut.Save()

One of the google search: Create shortcut with Unicode character


Thank you for your prompt reply, then I tried.

$tarpath = "C:\f[o]o♭\pr[o]file♭.txt"
$lnkpath = "C:\b[a]r♭\pr[o]file♭.txt"

$tarpath = $tarpath.Replace('[','`[')
$tarpath = $tarpath.Replace(']','`]')
$lnkpath = $lnkpath.Replace('[','`[')
$lnkpath = $lnkpath.Replace(']','`]'

# echo print $tarpath like this -> C:\b`[a`]r♭\pr`[o`]file♭.txt,
# but I'm not sure when it is the same at -Path $tarpath

New-Item -ItemType SymbolicLink -Path $tarpath -Value $lnkpath
Community
  • 1
  • 1
Foolenko
  • 3
  • 2
  • You need to escape `[` and `]`: ```-Path C:\f`[o`]o♭\pr`[o`]file♭.txt``` – Mathias R. Jessen Apr 09 '17 at 12:04
  • thank you for your prompt reply, then I tried. $tarpath="C:\f[o]o♭\pr[o]file♭.txt" $lnkpath="C:\b[a]r♭\pr[o]file♭.txt" $tarpath=$tarpath.replace('[','`[') $tarpath=$tarpath.replace(']','`]') $lnkpath=$lnkpath.replace('[','`[') $lnkpath=$lnkpath.replace(']','`]') echo print like this -> ``C:\b`[a`]r♭\pr`[o`]file♭.txt``, but I'm not sure when it is the same at -Path $tarpath New-Item -ItemType SymbolicLink -Path $tarpath -Value $lnkpath – Foolenko Apr 09 '17 at 12:55
  • Backticks in comments are a nightmare, [edit](http://stackoverflow.com/posts/43306251/edit) your question instead :-) – Mathias R. Jessen Apr 09 '17 at 12:59
  • Only the `-Path` argument to `New-Item` needs to have them escaped, not the `-Value` argument – Mathias R. Jessen Apr 09 '17 at 13:03
  • @Mathias R. Jessen thank you, it finally work, thank for your help. =] – Foolenko Apr 09 '17 at 13:06
  • You're welcome :-) Added a proper answer – Mathias R. Jessen Apr 09 '17 at 13:15

1 Answers1

3

Whenever you see a -Path parameter on a builtin cmdlet, assume that it supports wildcard globbing.

This means that [x] is not interpreted literally, but as a simple character class substitute. In your example, that means C:\f[o]o♭\pr[o]file♭.txt is interpreted as C:\foo♭\profile♭.txt which is why it complains that the target path doesn't exist.

To get around this, escape the square brackets with a backtick (`):

New-Item -ItemType SymbolicLink -Path 'C:\f`[o`]o♭\pr`[o`]file♭.txt' -Value "C:\b[a]r♭\pr[o]file♭.txt"
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206