1

I can't figure out how to add diacritics to the file name and target paths of shortcuts.

$ws_shell = New-Object -COMObject WScript.Shell
$shortcut = $ws_shell.CreateShortcut("Yū-Sibu.lnk")
$shortcut.TargetPath = "Yū-Sibu"

This code outputs a shortcut with the path 'Yu-Sibu.lnk' and targets 'Yu-Sibu'.

Even Write-Host $shortcut.TargetPath returns 'Yu-Sibu'.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • see this: http://stackoverflow.com/questions/1056692/how-to-encode-unicode-character-codes-in-a-powershell-string-literal – Avshalom Sep 07 '16 at 09:05
  • Even today, I'd strongly recommend against using non-ASCII characters in file names. – vonPryz Sep 07 '16 at 10:39
  • 1
    Billion(s) of people use non-ASCII file names so it's perfectly fine. As for the problem, you're using an antediluvian dinosaur derelict WScript. See C# example of creating a unicode shortcut lnk: [Create shortcut with Unicode character](https://stackoverflow.com/a/13542488) – wOxxOm Sep 07 '16 at 11:47

1 Answers1

1

WScript is ancient and can't properly deal with unicode, so we'll use the newer ShellLink interface. Since it can only modify existing lnk files, we'll create a temporary empty lnk with WScript first.

function Create-Lnk(
    [Parameter(Mandatory=$true)]
    [ValidateScript({Test-Path -IsValid -Literal $_})]
    [string]$lnk,

    [Parameter(Mandatory=$true)]
    [ValidateScript({Test-Path -Literal $_})]
    [string]$target,

    [string]$arguments = '',
    [string]$description = '',
    [string]$workingDir = '',

    [ValidateSet('normal', 'minimized', 'maximized')]
    [string]$windowState
) {
    $tmpName = [IO.Path]::GetRandomFileName() + '.lnk'
    $tmpFolder = $env:TEMP
    $tmpFullPath = Join-Path $tmpFolder $tmpName

    $ws_shell = New-Object -com WScript.Shell
    $shortcut = $ws_shell.CreateShortcut($tmpFullPath)
    $shortcut.Save()

    $shellApp = New-Object -com Shell.Application
    $shellLink = $shellApp.NameSpace($tmpFolder).ParseName($tmpName).GetLink()

    $shellLink.Path = $target
    $shellLink.Arguments = $arguments
    $shellLink.Description = $description 
    $shellLink.WorkingDirectory = $workingDir 
    $shellLink.ShowCommand = switch($windowState){'minimized'{2} 'maximized'{3} default{1}}
    $shellLink.Save()

    move -literal $tmpFullPath $lnk -force
}

Example:

Create-Lnk -lnk (Join-Path ([Environment]::GetFolderPath('Desktop')) 'пишижиши.lnk') `
           -target 'D:\lost in translation\なんでやねん!.txt' `
           -description '¿Por qué dices eso?'
wOxxOm
  • 65,848
  • 11
  • 132
  • 136