How to run a PowerShell script from a console window when there is Å, Ä or Ö in the search path?

When I right click on a script.ps1 file and select "Run with PowerShell", a PS console window comes up, and my script can't run for some reason. So I get an error, but I can't actually read the message because the window closes right after the error comes up.

So I would like to open a new PowerShell console window, navigate to the script file, run it, and hopefully this time get the luxury of reading the stupid error message. So how do I run the script from the console window?

  • OS version: Windows 8.0
  • PS version: 3.0

Simply typing in script.ps1 or the suggested ".\script.ps1" just doesn't do anything.

a

So I'm guessing it needs to get a command of some sort? The script is a really simple one, it's supposed to create new folders and name them by reading the Export.txt file.

$folder="C:\test";
$txtFile="C:\test\Export.txt";

get-content $txtFile | %{

    if($_ -match "^(\d+) (\w+)$")
    {
        mkdir "$folder\$_";
    }
}

Note! Please note that the error message above (screenshot) is not related to running the actual script. The only way I can run the script is by right clicking on the script file inside Explorer and selecting "Run with PowerShell" and the error I get from running it is a different one, not the one you see here above.

Update! See my second comment below. For paths like C:\test\script.ps1 it does work with .\script.ps1. I still need help on running PS script file for when the path to the script file includes funky letters like Å, Ä and Ö.

Update - Å, Ä and Ö breaks PS

Like I said I seem to have issues with these characters in PS. That's why I initially thought I was not doing it right. So here I'll give you two examples to demonstrate this.

Running a PS script without Å, Ä and Ö

This is my script.

$folder="C:\test";
$txtFile="C:\test\Export.txt";

get-content $txtFile | %{

    if($_ -match "^(\d+) (\w+)$")
    {
        mkdir "$folder\$_";
    }
}

These are the contents of the location where I am testing it now.

PS C:\test> dir


    Directory: C:\test


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2014-02-28     09:12       3648 Export.txt
-a---        2014-02-28     10:49        162 script.ps1


PS C:\test>

This is me running the script from PS console window.

PS C:\test> .\script


    Directory: C:\test


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        2014-02-28     10:55            001 Harberget
d----        2014-02-28     10:55            004 Badstuknappen
d----        2014-02-28     10:55            005 Aspberget
d----        2014-02-28     10:55            006 Knipen
d----        2014-02-28     10:55            011 Juberget
d----        2014-02-28     10:55            012 Rangen
d----        2014-02-28     10:55            014 Grasbricka
d----        2014-02-28     10:55            018 Letafors
d----        2014-02-28     10:55            020 Rya
d----        2014-02-28     10:55            022 Bograngen
d----        2014-02-28     10:55            023 Dalby
d----        2014-02-28     10:55            029 Transtrand
d----        2014-02-28     10:55            032 Viggen
d----        2014-02-28     10:55            033 Nyskoga
d----        2014-02-28     10:55            044 Vitsand
d----        2014-02-28     10:55            059 Lekvattnet
d----        2014-02-28     10:55            064 Gustavsfors
d----        2014-02-28     10:55            066 Tyfors
d----        2014-02-28     10:55            067 Mitandersfors
d----        2014-02-28     10:55            070 Bada
d----        2014-02-28     10:55            073 Geijersholm
d----        2014-02-28     10:55            074 Liljendal
d----        2014-02-28     10:55            077 Bogen
d----        2014-02-28     10:55            079 Grinnemo
d----        2014-02-28     10:55            080 Lysvik
d----        2014-02-28     10:55            082 Uddeholm
d----        2014-02-28     10:55            085 Vitklinten
d----        2014-02-28     10:55            086 Gravendal
d----        2014-02-28     10:55            087 Magnor
d----        2014-02-28     10:55            089 Bortan
d----        2014-02-28     10:55            090 Fredros
d----        2014-02-28     10:55            095 Busken
d----        2014-02-28     10:55            101 Charlottenberg
d----        2014-02-28     10:55            102 Allstakan
d----        2014-02-28     10:55            104 Rotten
d----        2014-02-28     10:55            105 Sunne
d----        2014-02-28     10:55            106 Annefors
d----        2014-02-28     10:55            107 Munkfors
d----        2014-02-28     10:55            108 Sunnemo
d----        2014-02-28     10:55            109 Nordmarker
d----        2014-02-28     10:55            111 Silvergruva
d----        2014-02-28     10:55            113 Skillingemark
d----        2014-02-28     10:55            115 Gunnarsskog
d----        2014-02-28     10:55            116 Mangskog
d----        2014-02-28     10:55            118 Rottneros
d----        2014-02-28     10:55            123 Persberget
d----        2014-02-28     10:55            124 Hellefors
d----        2014-02-28     10:55            125 Bottnar
d----        2014-02-28     10:55            128 Ottebol
d----        2014-02-28     10:55            129 Rackstad
d----        2014-02-28     10:55            130 Gullesbyn
d----        2014-02-28     10:55            131 Emtervik
d----        2014-02-28     10:55            135 Brattfors
d----        2014-02-28     10:55            136 Filipstad
d----        2014-02-28     10:55            137 Saxhyttan
d----        2014-02-28     10:55            138 Orrkullen
d----        2014-02-28     10:55            142 Arvika
d----        2014-02-28     10:55            143 Edane
d----        2014-02-28     10:55            144 Brunsberg
d----        2014-02-28     10:55            148 Nyed
d----        2014-02-28     10:55            149 Hedenskog
d----        2014-02-28     10:55            150 Kroppa
d----        2014-02-28     10:55            151 Loka
d----        2014-02-28     10:55            152 Dalen
d----        2014-02-28     10:55            154 Karlanda
d----        2014-02-28     10:55            156 Glava
d----        2014-02-28     10:55            158 Boda
d----        2014-02-28     10:55            159 Frykerud
d----        2014-02-28     10:55            160 Frykestad
d----        2014-02-28     10:55            161 Forshaga
d----        2014-02-28     10:55            164 Storfors
d----        2014-02-28     10:55            167 Holmedal
d----        2014-02-28     10:55            169 Lenungen
d----        2014-02-28     10:55            172 Vermskog
d----        2014-02-28     10:55            173 Edsvalla
d----        2014-02-28     10:55            174 Grava
d----        2014-02-28     10:55            175 Alster
d----        2014-02-28     10:55            176 Glumserud
d----        2014-02-28     10:55            177 Kungskog
d----        2014-02-28     10:55            178 Lundsberg
d----        2014-02-28     10:55            180 Mona
d----        2014-02-28     10:55            181 Foxen
d----        2014-02-28     10:55            182 Silbodal
d----        2014-02-28     10:55            183 Sannaneknatten
d----        2014-02-28     10:55            186 Borgvik
d----        2014-02-28     10:55            187 Grums
d----        2014-02-28     10:55            188 Eriksberg
d----        2014-02-28     10:55            189 Karlstad
d----        2014-02-28     10:55            193 Alkvettern
d----        2014-02-28     10:55            194 Trankil
d----        2014-02-28     10:55            195 Blomskog
d----        2014-02-28     10:55            196 Sillerud
d----        2014-02-28     10:55            198 Gillberga
d----        2014-02-28     10:55            199 Segmon
d----        2014-02-28     10:55            200 Ed
d----        2014-02-28     10:55            204 Presterud
d----        2014-02-28     10:55            205 Kristinehamn
d----        2014-02-28     10:55            207 Salholmen
d----        2014-02-28     10:55            208 Gustavsfors
d----        2014-02-28     10:55            211 Kila
d----        2014-02-28     10:55            212 Hammar
d----        2014-02-28     10:55            215 Skallholmen
d----        2014-02-28     10:55            217 Posseberg
d----        2014-02-28     10:55            219 Degerfors
d----        2014-02-28     10:55            220 Torrskog
d----        2014-02-28     10:55            221 Suled
d----        2014-02-28     10:55            222 Svanskog
d----        2014-02-28     10:55            223 Tveta
d----        2014-02-28     10:55            224 Seffle
d----        2014-02-28     10:55            226 Kilsbyn
d----        2014-02-28     10:55            227 Visnum
d----        2014-02-28     10:55            228 Nysund
d----        2014-02-28     10:55            233 Rudskoga
d----        2014-02-28     10:55            234 Grytfors
d----        2014-02-28     10:55            237 Millesvik
d----        2014-02-28     10:55            240 Rosenborg

Evidently the script is running. However, it only creates 116 folders out of the total of 245 folder it's supposed to create. That might have to do with the script itself. It is skipping lines such as 002 Långflon and 003 Källegrafsrös because they contain the letter Å, Ä and/or Ö. Any ideas? But the point is that the script is running in PS console window as expected.

Running the same PS script with Å, Ä and Ö

I have intentionally thrown in an Ä in the folder name. So test has been renamed to täst. I have updated the script file with these changes, and this is my script now.

$folder="C:\täst";
$txtFile="C:\täst\Export.txt";

get-content $txtFile | %{

    if($_ -match "^(\d+) (\w+)$")
    {
        mkdir "$folder\$_";
    }
}

There are no folders in the test folder right now. I have removed them to re-run the test.

PS C:\täst> dir


    Directory: C:\täst


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2014-02-28     09:12       3648 Export.txt
-a---        2014-02-28     11:04        164 script.ps1


PS C:\täst>

And again, this is me running the updated script file.

PS C:\täst> .\script.ps1
get-content : Cannot find path 'C:\täst\Export.txt' because it does not exist.
At C:\täst\script.ps1:4 char:1
+ get-content $txtFile | %{
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\täst\Export.txt:String) [Get-Content], ItemNotFoun
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

PS C:\täst>

Cannot find path 'C:\täst\Export.txt' because it does not exist.

As you can see, it is trying to find a path that's not there. Point proven. Characters Å, Ä and Ö break PS! But why? What am I doing that's so wrong?...


Solution 1:

Here you seem to have managed to enter the directory name using UTF-8 (character set):

$folder="C:\täst";

The umlaut is being stored as two characters in the script. That is why you see it as two characters in the error message:

get-content : Cannot find path 'C:\täst\Export.txt' because it does not exist.

Exactly how did you "intentionally throw in an Ä in the folder name"? You need to do that as ASCII, not as UTF-8.

Display your script using the "type" command; I am sure you will then see the UTF representation like in the error message. Your editor is displaying the umlaut for you as you expect. The type command will [most likely] show the umlaut as two characters like in the error message.

Ensure your editor is saving pure ASCII or ANSI.

Solution 2:

Running a PowerShell script

Suppose you have a PowerShell script called my script.ps1 stored in C:\Some folder. In order to run it you can either:

  • Navigate to the actual path, and then execute it:

     cd "C:\Some folder"
     & ".\my script.ps1"
    
  • Execute it by specifying the full path directly:

     & "C:\Some folder\my script.ps1"
    

& is the call operator. As an alternative you can start it from a regular command prompt:

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -File "C:\Some folder\my script.ps1"

Fixing the script

I've rewritten the script so that it will accepts two arguments: the input file (which is assumed to be a text saved as UTF-8), and the destination folder. When no encoding is specified it will defaults to ASCII.

if ($args.Count -gt 1)
{
    $file=$args[0];
    $dest=$args[1];
    Get-Content $file -Encoding UTF8 | %{ md "$dest\$_" >$null; }
}

References

  • Running Windows PowerShell Scripts
  • More Powerful Ways to Launch Windows PowerShell
  • Get-Content for FileSystem