Goto Top

Powershell Folder Picker Dialog

Hello Guys face-smile

i'm just thinking about to implement a folder picking dialog box in my existing script.
In my existing script i want to install fonts on a remote machine. This script is working fine, but i need to write the folder path harcoded into the script.
Now i want to change this.

This is my script which is installing the fonts.
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
if (!$myWindowsPrincipal.IsInRole($adminRole)){
    start-process "powershell" -Verb "runas" -ArgumentList "-File",$MyInvocation.MyCommand.Definition
$name = read-host "give hostname"
PsExec.exe \\$Name -s winrm.cmd quickconfig -q
#Computers to install fonts to
$ComputerArray = @($Name)
#A Share containing only the fonts you want to install
#Wil be created on remote Pc if not exists, fonts will be copied here and deleted after install.
foreach ($pc in $ComputerArray) {
If((Test-Connection -ComputerName  $pc -Count  1 -ErrorAction SilentlyContinue) -and ($Winrmstatus=Test-WSMan -ComputerName $pc -ErrorAction SilentlyContinue)) {
$RemotePcStagingDir = "\\$pc\$($PcStagingDir.replace(':','$'))"
$ServiceName = @("WinRM")
foreach ($svc in $ServiceName) {
        while(((Get-Service -ComputerName $pc -Name $svc -ErrorAction SilentlyContinue).status -ne "Running")-and($i -lt 10)){
            if((Get-Service -ComputerName $pc -Name $svc -ErrorAction SilentlyContinue).StartType -eq "Disabled"){
            Write-Host "Try $i , Setting service $svc StartType to Manual on $pc ..."
            Set-Service -ComputerName $pc -Name $svc -StartupType Manual -ErrorAction SilentlyContinue}
        Write-Host "Try $i / 10 , Starting $svc Service on $pc ..."
        $commandz="sc \\"+$pc +" Start "+$svc
        & cmd.exe /c $commandz | Out-Null
        sleep 3}
        if($i -ge 10){break}
if($i -ge 10){Write-Host "Could NOT start service $svc, Skipping Computer $pc" -ForegroundColor Red}else{
if ( -not (Test-Path -Path $RemotePcStagingDir)){New-Item -Path $RemotePcStagingDir -ItemType Directory -Force}
$RemoteWinDir=Invoke-Command -ComputerName $pc -ScriptBlock {return $env:windir}
foreach($FontFile in (Get-ChildItem -file -path $FontDir)){
    if(-not(Test-Path "\\$pc\$($RemoteWinDir.replace(':','$'))\Fonts\$FontFile")){
        Copy-Item "$FontDir\$FontFile" -Destination $RemotePcStagingDir -Force
        Invoke-Command -ComputerName $pc -ScriptBlock {
       $fontRegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
       $fontsFolderPath = "$($env:windir)\fonts"
    # Create hashtable containing valid font file extensions and text to append to Registry entry name.
    $hashFontFileTypes = @{}
    $hashFontFileTypes.Add(".fon", "")
    $hashFontFileTypes.Add(".fnt", "")
    $hashFontFileTypes.Add(".ttf", " (TrueType)")
    $hashFontFileTypes.Add(".ttc", " (TrueType)")
    $hashFontFileTypes.Add(".otf", " (OpenType)")
        [string]$filePath = (Get-Item $filePath).FullName
        [string]$fileDir  = split-path $filePath
        [string]$fileName = split-path $filePath -leaf
        [string]$fileExt = (Get-Item $filePath).extension
        [string]$fileBaseName = $fileName -replace($fileExt ,"")
        $shell = new-object -com shell.application
        $myFolder = $shell.Namespace($fileDir)
        $fileobj = $myFolder.Items().Item($fileName)
        $fontName = $myFolder.GetDetailsOf($fileobj,21)
        if ($fontName -eq "") { $fontName = $fileBaseName }
        copy-item $filePath -destination $fontsFolderPath
        $fontFinalPath = Join-Path $fontsFolderPath $fileName
        if (-not($hashFontFileTypes.ContainsKey($fileExt))){Write-Host "File Extension Unsupported";$retVal = 0}
        if ($retVal -eq 0) {
            Write-Host "Font `'$($filePath)`'`' installation failed on $env:computername" -ForegroundColor Red
            Write-Host ""
            Set-ItemProperty -path "$($fontRegistryPath)" -name "$($fontName)$($hashFontFileTypes.$fileExt)" -value "$($fileName)" -type STRING
            Write-Host "Font `'$($filePath)`' $fontName $($hashFontFileTypes.$fileExt) installed successfully on $env:computername" -ForegroundColor Green
        Write-Host "An error occured installing `'$($filePath)`' on $env:computername" -ForegroundColor Red
        Write-Host "$($error.ToString())" -ForegroundColor Red
     Remove-Item "$RemotePcStagingDir\$FontFile" -ErrorAction SilentlyContinue
foreach($DisabledSvc in $DisabledSvcs){
    Write-Host "Setting service $DisabledSvc StartType to Disabled on $pc ..."
    Set-Service -ComputerName $pc -Name $DisabledSvc -StartupType Disabled -ErrorAction SilentlyContinue
    $commandz="sc \\"+$pc +" Stop "+$DisabledSvc
    & cmd.exe /c $commandz | Out-Null
}else{if($Winrmstatus){Write-Host "No Connection to Remote Pc $pc" -ForegroundColor Red}Else{Write-Host "No Connection to WinRM service on Remote Pc $pc" -ForegroundColor Red}}

In line 13, i'm trying to change it with the following folder picker dialog.
Function Get-Folder($initialDirectory)
    $Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
    $Ordnername.Description = "Ordner auswählen"
    $Ordnername.rootfolder = "MyComputer"
    if($Ordnername.ShowDialog() -eq "OK")
        $Ordner += $Ordnername.SelectedPath
    return $Ordner
$folder = Get-Folder

Replacing the hardcoded folder with the
does not work. There is no error message.

Content-Key: 658826


Printed on: June 5, 2023 at 02:06 o'clock

Member: GarfieldBonn
GarfieldBonn Mar 04, 2021, updated at May 16, 2023 at 13:17:19 (UTC)
Goto Top

should it no be

$FontDir = Get-Folder
Member: natalie.solero
natalie.solero Mar 04, 2021, updated at May 16, 2023 at 13:17:19 (UTC)
Goto Top
Unfortunately i't doesn't work. The script closes directly without any error message.
Member: GarfieldBonn
GarfieldBonn Mar 04, 2021, updated at May 16, 2023 at 13:17:19 (UTC)
Goto Top
i tested it up to the foreach loop. It works fine.

How do you bind the function? Same script or via include-path?

The scipt works fine withput the function? Then it must be how how you bind it to the rest.