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

3

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?...

Samir

Posted 2014-02-28T08:43:35.223

Reputation: 17 919

Don't use quotes when typing .\script.ps1. It would just echo your command out. – Ashtray – 2014-02-28T08:46:21.267

So .\script.ps1 will run it? But says type ".\script.ps1" and that means to include the quote marks, no? – Samir – 2014-02-28T08:48:31.933

1Oh! So it does work then. Thanks! It's just that it does not work with my own search path. I have a \\FILESERVER\path\path\path mounted as Z: drive. It includes the letter Ä and that's a reason enough for PS not to work? It doesn't seem to like these letters at all. The script above doesn't even want to create new folders if the name is set to include letters Å, Ä and Ö. – Samir – 2014-02-28T09:16:19.297

So I'm not really sure this answers my question, it kind of... sort of... does work with .\filename.ps1 as long as the path does not include funky characters like Ä. – Samir – 2014-02-28T09:17:35.130

You may find this technet article useful: http://technet.microsoft.com/en-us/library/ee176949.aspx

– wmz – 2014-02-28T09:26:58.750

@Sammy The reason the script dont create some of the folders is because I created the original script to match only the names you first provided (i.e 3 digits, space and then a-z characters). See the updated answer to you original question for a pattern that matches all your names. http://superuser.com/questions/722372/how-to-create-multiple-folders-and-name-them-by-reading-lines-from-text-file/722379#722379

– Martin – 2014-02-28T13:04:18.023

@Martin Yes, I can confirm that your script now works on this Windows 8 computer as well. But I'm a bit confused here... I tested your original script yesterday on my Vista computer and it worked. I'm not sure though if it created all the folders but it created a whole lot of them so I didn't count. What difference does it make if it's Windows Vista or Windows 8?... Also, is this at least in part encoding issue or not at all? See my comments to UnlimitedInfinity's answer below. – Samir – 2014-02-28T13:20:10.283

@Sammy, the problem with European characters is solved with encoding your script in ANSI. You need to use another "match pattern" for extracting the file names from your input like in Martin's answer (in the other post). – UnlimitedInfinity – 2014-02-28T13:30:20.267

Martin's new script is working. However, it works from C:\test and it works from Z:\test but it does not work from Z:\S_Värmland\test. The C is my local disk drive, and Z is a network location, (\\FILESERVER) mounted as Z drive. So it looks like I'm right about the search path issue anyway, it doesn't like the Ä in the search path? – Samir – 2014-02-28T13:42:42.277

@Sammy, run type theNewScriptName. Do you see an Umlaut or two "wierd" characters? In other words, are you sure that you saved the script in ANSI? – UnlimitedInfinity – 2014-02-28T14:28:47.580

Answers

3

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.

UnlimitedInfinity

Posted 2014-02-28T08:43:35.223

Reputation: 293

My "editor" was Windows Explorer. If that helps? I simply renamed the folder from "test" to "täst" in Explorer. As for the script it was saved with Notepad++ with UTF without BOM. – Samir – 2014-02-28T11:17:20.630

@Sammy Use ANSI, not UTF. Pretty much never save a script in UTF. UTF for your data, ANSI for your scripting. – UnlimitedInfinity – 2014-02-28T11:24:47.977

How do you ensure "pure ASCII" is being used in Notepad++? I can only get ANSI, and as I understand it this is in fact Microsoft version of ASCII. But I could be wrong. – Samir – 2014-02-28T12:48:33.140

@Sammy ANSI and ASCII are similar enough that I have always considered them to be the same. I just now asked google and there are some differences: http://www.differencebetween.net/technology/web-applications/difference-between-ansi-and-ascii/ The answer now has both.

– UnlimitedInfinity – 2014-02-28T12:52:42.720

1@Sammy To save as ANSI in Notepad++, click the menu Encoding --> Encode in ANSI. – Martin – 2014-02-28T12:55:58.000

This was helpful to the extent that I now have the PS script creating 237 folders, out of the total 245. It is still skipping some of them. Don't know why. But having the list in UTF-8 (with BOM) and the script in ANSI (ASCII) did help. – Samir – 2014-02-28T12:59:53.317

The complete list of items in the list can be found at http://pastebin.com/SNde4bBN

– Samir – 2014-02-28T13:00:27.303

The items that are being skipped from the list are those with an extra space, such as 021 Södra Finnskoga as opposed to 020 Rya which only has one space right after the digits. – Samir – 2014-02-28T13:03:21.987

That is due to the match pattern. New match pattern here: http://superuser.com/questions/722372/how-to-create-multiple-folders-and-name-them-by-reading-lines-from-text-file/722379#722379

– UnlimitedInfinity – 2014-02-28T13:32:59.857

1

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

and31415

Posted 2014-02-28T08:43:35.223

Reputation: 13 382

Hey, that's really handy. Did not know of this syntax. Thanks! – Ashtray – 2014-02-28T09:23:19.233

Yes, I have tested now, the ".script.ps1alone doesn't work. But & ".\script.ps1" does, and so does .\script.ps1. But this however, does not help me with the issues I am having with Å, Ä and Ö characters. Why won't PS understand these characters? I will post an update to my question shortly, with examples. – Samir – 2014-02-28T09:43:35.293

I have updated my question with examples. If you can expand your answer and explain how to run a script in PS when there are non-ASCII characters in the search path, I might accept it as a solution. Or is this actually related to the script itself (get-content $txtFile)? – Samir – 2014-02-28T10:19:57.617

You mean like get-content $txtFile | %{{mkdir "$folder\$_";}}? That only outputs mkdir "$folder\$_"; repeatedly 245 times in PS window, it doesn't create any folders. – Samir – 2014-02-28T11:02:32.310

I noticed the original script wouldn't work properly with things like 021 Södra Finnskoga because there's a space in the name. Either way, having the script like $folder="C:\täst"; $txtFile="C:\täst\Export.txt"; get-content $txtFile | %{mkdir "$folder\$_";} works for me. What are you using to save/edit the script and the list? This looks like an encoding issue. – and31415 – 2014-02-28T11:15:49.280

Yes. It sure looks like an encoding issue. Please see my answer. – UnlimitedInfinity – 2014-02-28T11:21:34.713

@Sammy I've update my answer to provide additional information. Let me know if you have any further doubts. – and31415 – 2014-03-01T15:43:31.993

-1

You have to save the file as UTF-8 with BOM - you can use Notepad++. Powershell is broken and cannot reliably read files that are UTF-8 without a BOM - go figure.

Same issue + solution as described above: https://stackoverflow.com/a/14482588/1819480

Infinum

Posted 2014-02-28T08:43:35.223

Reputation: 101