A PowerShell is just a text file containing PowerShell code. If the text file executes, the PowerShell interpreter interprets and executes its statements line by row. The PowerShell script is much like a batch file on the previous CMD console. You can create PowerShell scripts with very simple text-editing tools.
Create a script by redirecting
If your script is not very long, you can even redirect to a script file directly from the statement you want to execute in the console.
PS e:> ' Hello,powershell script ' > Myscript.ps1
ps e:>. \myscript.ps1
Hello,powershell Script
The downside is that your code must be enclosed in closed quotes. This is a very painful way to write, once you have quotes inside the script. You might even want to wrap the line in a script. The following here-strings example is good, which is to close the script file through @ ' @ ' @
PS e:> @ '
>> get-date
>> $Env: commonprogramfiles
>> #Script end
>> "files Count "
>> (LS). Count
>> #Script really end
>>
>> ' @ > Myscript.ps1
>>
PS e:>. Myscript.ps1
April 27, 2012 8:15:10
C:\Program Files\Common Files
files Count
20
Here-string begins with @ ' and ends with ' @. Any text can be stored inside, even some special characters, empty numbers, white spaces. But if you accidentally write single quotes in double quotes, PowerShell will parse the variables inside.
Creating a script from the editor
In fact, it is very convenient or the most authentic version of the editor Notepad, you can directly open Notepad in the PowerShell console
PS e:> notepad.exe. \MYSCRIPT.PS1
PS e:> notepad.exe
Remember to save it after editing.
Run PowerShell Script
When your script is written successfully, you may run it the first time as follows, that is, just enter the script file name and the error.
The "MYSCRIPT.PS1" item cannot be recognized as a cmdlet, function, script file, or the name of a running program. Please check the spelling of the name, if included
Path, make sure the path is correct, and then try again.
Location: 1 Characters: 13
+ Myscript.ps1 < <<<
+ Categoryinfo:objectnotfound: (MyScript.ps1:String) [], commandnotfoundexception
+ fullyqualifiederrorid:commandnotfoundexception
Suggestion [3,general]: The command myscript.ps1 was not found, but it does exist in the current location. Windows PowerShell Default
The command is not loaded from the current location. If you trust this command, type instead. Myscript.ps1 ". For more details, see the Get-h
ELP about_command_precedence ".
Unless you use a relative path, or an absolute path
PS e:>. \myscript.ps1
April 27, 2012 8:33:03
C:\Program Files\Common Files
files Count
PS E: > E:myscript.ps1
April 27, 2012 8:33:11
C:\Program Files\Common Files
files Count
20
Enforce policy restrictions
Script execution is blocked in PowerShell general initialization. The execution of the script depends on the execution policy of the PowerShell.
File E:MYSCRIPT.PS1 cannot be loaded because scripts are not allowed in this system. For more information, see the Get-help about_sign
ing ".
Location: 1 Characters: 15
+ . Myscript.ps1 < <<<
+ categoryinfo:notspecified: (:) [], Pssecurityexception
+ fullyqualifiederrorid:runtimeexception
Only administrators have permission to change this policy. Non-administrators will complain.
To view the script execution policy, you can pass:
PS e:> Get-executionpolicy
To change the script execution policy, you can use the
PS e:> get-executionpolicy
restricted
PS e:> set-executionpolicy Unrestricted
Performing policy changes
Executing a policy can prevent you from executing untrusted scripts. Changing the execution policy may cause you to face about_execution_policies
The security risks described in the Help topic. Do you want to change the execution policy?
[Y] is (y) [n] No (n) [s]-pending (s) [?] Help (Default is "Y"): Y
The script execution policy type is: Microsoft.PowerShell.ExecutionPolicy
To view all supported execution policies:
PS e:> [System.enum]::getnames ([Microsoft.PowerShell.ExecutionPolicy])
Unrestricted
RemoteSigned
AllSigned
Restricted
Default
Bypass
Undefined
Unrestricted: Permissions are highest and can execute any script without restriction.
Default: Defaults to PowerShell: Restricted, no script execution allowed.
AllSigned: All scripts must be signed before they can be run.
remotesigned: Local scripts are unrestricted, but scripts from the network must be signed.
The signature on the PowerShell script is in the follow-up meeting.
Execute scripts like commands
How to execute a script like executing a command without entering the relative path or absolute path of the script, or even *.ps1 the extension.
Then save the execution statement of the script as an alias:
PS e:> Set-alias Invok-myscript. Myscript.ps1
PS e:> invok-myscript
April 28, 2012 0:24:22
C:\Program Files\Common Files
files count
20