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.
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?...
Don't use quotes when typing
.\script.ps1
. It would just echo your command out. – Ashtray – 2014-02-28T08:46:21.267So
.\script.ps1
will run it? But saystype ".\script.ps1"
and that means to include the quote marks, no? – Samir – 2014-02-28T08:48:31.9331Oh! 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 asZ:
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.297So 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.130You 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 fromZ:\test
but it does not work fromZ:\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