3

I'm trying to run this in one line using the "Run" command:

powershell -NoExit ; Get-ChildItem -Recurse -force -Include *.ost \\PcHostname\c$\users -ErrorAction "SilentlyContinue" | ls | Select-Object Name, @{Name="GigaBytes";Expression={$_.Length / 1GB}}

but I get this error:

GigaBytes : The term 'GigaBytes' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:137
+ ... t Name, @{Name=GigaBytes;Expression={$_.Length / 1GB}}
+                    ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (GigaBytes:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The code works on a started Powershell window without powershell -NoExit ;.

Any ideas to fix it?

Mike G
  • 4,232
  • 9
  • 40
  • 66
pedaleo
  • 401
  • 2
  • 7
  • 20
  • Just tried and didn't get any such error. – Rahul Jun 18 '14 at 12:04
  • @Rahul I mean, Start->Run "PasteTheCode" but I get the error... Did you tried the same steps? – pedaleo Jun 18 '14 at 13:42
  • That way you will get error most probably. Instead do start->run->powershell. once shell window comes up .. paste the coe and run. it will be fine – Rahul Jun 18 '14 at 13:46
  • @Rahul I need to change the hostname on the fly, another software is involved, thats why I looking that way. I want to believe theres something to get it work as I need:( – pedaleo Jun 18 '14 at 14:02

1 Answers1

3

Due to the way command line arguments are tokenized through "Run", you need to either escape the double quotes around GigaBytes (using the \ escape character) or substitute them with single quotes. Either of these two Select-Object commands should work:

Select-Object Name, @{Name=\"GigaBytes\";Expression={$_.Length / 1GB}}

or

Select-Object Name, @{Name='GigaBytes';Expression={$_.Length / 1GB}}

This is because anything wrapped in double quotes will be interpreted as a single argument.

See this answer for a more detailed explanation as to how command line arguments are tokenized through "Run".

Community
  • 1
  • 1
Kohlbrr
  • 3,861
  • 1
  • 21
  • 24