Ok, I browsed through the cheat sheet, and I feel that I am now ready to create my very first simple PowerShell script.
Opened up notepad to print a simple "Hello, World!" message.
Saved the file as sample.ps1, run Windows Powershell, changed my folder to where the script is, and ran
sample.ps1. What I got were errors:
File C:\scripts\sample.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-
help about_signing" for more details.
And
PS C:\scripts> sample.ps1
The term 'sample.ps1' 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:11
+ sample.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (sample.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command sample.ps1 was not found, but does exist in the current location. Windows PowerShell
doesn't load commands from the current location by default. If you trust this command, instead type ".\sample.ps1". See
"get-help about_Command_Precedence" for more details.
PS C:\scripts>
It turns out that running a PowerShell isn't how we're used to in running applications. Here's how to successfully run the script:
1) Set the execution policy to
RemoteSigned so you can run scripts, as the default is
Restricted.
2) As recommended by the error message above, we need to run the script indicating it's
full path, or if you're in the same folder where the script is,
prefix the script name with ".\"
3) Now if you want to run the script using the full path, and the path happens to have spaces (like C:\My Scripts\sample.ps1), then you'll run into the second error message above. If this is the case, make sure to
enclose the full path with double quotes and
prefix it with an ampersand (&).
PS C:\ scripts> & "C:\My Scripts\sample.ps1"
Reference:
http://technet.microsoft.com/en-us/library/ee176949.aspx
*joychua97