Avatar of David Sankovsky
David Sankovsky
Flag for Israel asked on

Powershell won't write hebrew characters in specific cases

Hi Experts,

I'm trying to write a PS Script that will allow me to create a folder on a users Desktop that will hold a set of shortcuts that will always open via Google Chrome regardless of which browser the user has set as a default one.

Also, since some of out users have chrome installed to a non-standard path due to specific restrictions I can't just use a set of shotcuts and copy them from a shared drive via GPO.


I resorted to writing a custom PS Script for that task,

The script will check if the folder exists on the desktop and will create it if not,

It will then iterate through a hash table of links and dames to create the shortcuts.

Here's the problem: the names of the shortcut have to be in Hebrew, as my users can't speak English to save their own lives if they had to!


While the part that created the desktop folder (Whose name is also in Hebrew mind you) works perfectly, the shortcut creaton logic refuses to read or write hebrew characters and doesn't create the shortcuts:


$chexec = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" -Name "(default)"
$chpath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" -Name "Path"
$DesktopPath = [Environment]::GetFolderPath("Desktop") $shortdirpath = $DesktopPath + "\קיצורי דרך מטמון" if (!(Test-Path $shortdirpath)) {     New-Item -ItemType Directory -Force -Path $shortdirpath } $shortcuts = @(     [pscustomobject]@{url="https://ynet.co.il";name="חדשות"},     [pscustomobject]@{url="https://google.com";name="חיפוש"} ) foreach ($shortcut in $shortcuts) {     #Write-Host $shortcut.name     $shortcutpath = ($shortdirpath)+ '\' +($shortcut.name)+".lnk"     $targetFile = '"'+$chexec+'"'     $url = $shortcut.url     $WshShell = New-Object -ComObject WScript.Shell     $shtcut = $WshShell.CreateShortcut($shortcutpath)     $shtcut.TargetPath = $targetFile     $shtcut.Arguments = $url     $shtcut.WorkingDirectory = $chpath     $shtcut }

Open in new window

The result is this: 

FullName         : C:\Users\davidsa\Desktop\?????? ??? ?????\?????.lnk
Arguments        : https://ynet.co.il
Description      :
Hotkey           :
IconLocation     : ,0
RelativePath     :
TargetPath       : C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
WindowStyle      : 1
WorkingDirectory : C:\Program Files (x86)\Google\Chrome\Application

FullName         : C:\Users\davidsa\Desktop\?????? ??? ?????\?????.lnk
Arguments        : https://google.com
Description      :
Hotkey           :
IconLocation     : ,0
RelativePath     :
TargetPath       : C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
WindowStyle      : 1
WorkingDirectory : C:\Program Files (x86)\Google\Chrome\Application


But if I only ask the system to write the names without the entire shortcut creation logic, it works fine:

$chexec = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" -Name "(default)"
$chpath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" -Name "Path"
$DesktopPath = [Environment]::GetFolderPath("Desktop") $shortdirpath = $DesktopPath + "\קיצורי דרך מטמון" if (!(Test-Path $shortdirpath)) {     New-Item -ItemType Directory -Force -Path $shortdirpath } $shortcuts = @(     [pscustomobject]@{url="https://ynet.co.il";name="חדשות"},     [pscustomobject]@{url="https://google.com";name="חיפוש"} ) foreach ($shortcut in $shortcuts) {     $shortcut.name }

Open in new window


חדשות
חיפוש

Powershell* Unicode

Avatar of undefined
Last Comment
David Sankovsky

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
David Sankovsky

ASKER
Revised Solution based on Ryan's Idea:

$chexec = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" -Name "(default)"
$chpath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" -Name "Path"

$DesktopPath = [Environment]::GetFolderPath("Desktop")
$shortdirpath = $DesktopPath + "\shortcuts"
if (!(Test-Path $shortdirpath))
{
    New-Item -ItemType Directory -Force -Path $shortdirpath
}

$shortcuts = @(
    [pscustomobject]@{url="https://ynet.co.il";ename="News";name="חדשות"},
    [pscustomobject]@{url="https://google.com";ename="Search";name="חיפוש"}
)
foreach ($shortcut in $shortcuts)
{
    #Write-Host $shortcut.name
    $shortcutpath = ($shortdirpath)+ '\' +($shortcut.ename)+".lnk"
    $targetFile = '"'+$chexec+'"'
    $url = $shortcut.url
    $WshShell = New-Object -ComObject WScript.Shell
    $shtcut = $WshShell.CreateShortcut($shortcutpath)
    $shtcut.TargetPath = $targetFile
    $shtcut.Arguments = $url
    $shtcut.WorkingDirectory = $chpath
    #$shtcut
    $shtcut.save()
    $hname = $shortcut.name+".lnk"
    Rename-Item -Path $shortcutpath -NewName $hname
}
Rename-Item -Path $shortdirpath -NewName "קיצורי דרך מטמון"

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck