Microsoft Windows PowerShell & Windows PowerShell ISE


PowerShell

What is Windows PowerShell?


Note:  The following information was found at Microsoft’s TechNet Website within the “Windows PowerShell User’s Guide” 


Windows PowerShell is a task-based command-line shell and scripting language designed especially for system administration.  Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.

Windows PowerShell commands, called cmdlets, let you manage the computers from the command line. Windows PowerShell providers let you access data stores, such as the registry and certificate store, as easily as you access the file system. In addition, Windows PowerShell has a rich expression parser and a fully developed scripting language.

This post will provide an overview of the features that are included with Windows PowerShell, and the graphical Windows PowerShell editor, Windows PowerShell Integrated Scripting Environment (ISE).


Use any of the following methods to start the installed version of Windows PowerShell 3.0, or Windows PowerShell 4.0, where applicable.  Click on this link to jump down to Windows PowerShell ISE.


How to Start PowerShell


From the Start Menu:

  • Click Start => type PowerShell => click Windows PowerShell
    or
  • From the Start menu => click Start => click All Programs => click Accessories => click the Windows PowerShell folder => click Windows PowerShell


At the Command Prompt:

  • In Cmd.exe, Windows PowerShell, or Windows PowerShell ISE, to start Windows PowerShell, type:  PowerShell


With Administrative privileges (“Run as administrator”):

  • Click Start => type PowerShell => right-click Windows PowerShell => click Run as administrator

Helpful Tips:
You can also use the Run dialog box, or type powershell in an open command-shell window. These techniques allow you to pass arguments to Windows PowerShell, including switches that control how Windows PowerShell works and parameters that execute additional commands.  For example, you can start Windows PowerShell in no-logo mode (meaning the logo banner is turned off) by using the startup command powershell -nologo. By default, when you start Windows PowerShell via the command shell, Windows PowerShell runs and then exits.  If you want Windows PowerShell to execute a command and not terminate, type powershell /noexit followed by the command text.


Syntax

PowerShell[.exe]
[-EncodedCommand <Base64EncodedCommand>]
[-ExecutionPolicy <ExecutionPolicy>]
[-InputFormat {Text | XML}]
[-Mta]
[-NoExit]
[-NoLogo]
[-NonInteractive]
[-NoProfile]
[-OutputFormat {Text | XML}]
[-PSConsoleFile <FilePath> | -Version <PowerShell version>]
[-Sta]
[-WindowStyle <style>]
[-File <FilePath> [<Args>]]
[-Command { - | <script-block> [-args <arg-array>]
              | <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

Parameters DEFINED

-EncodedCommand 
Accepts a base-64-encoded string version of a command. Use this parameter to submit commands to Windows PowerShell that require complex quotation marks or curly braces.

-ExecutionPolicy 
Sets the default execution policy for the current session and saves it in the $env:PSExecutionPolicyPreference environment variable. This parameter does not change the Windows PowerShell execution policy that is set in the registry. For information about Windows PowerShell execution policies, including a list of valid values, see about_Execution_Policies (http://go.microsoft.com/fwlink/?LinkID=135170).

-File []
Runs the specified script in the local scope ("dot-sourced"), so that the functions and variables that the script creates are available in the current session. Enter the script file path and any parameters. File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters and their values.

You can include the parameters of a script, and parameter values, in the value of the File parameter. For example: -File .\Get-Script.ps1 -Domain Central

Typically, the switch parameters of a script are either included or omitted. For example, the following command uses the All parameter of the Get-Script.ps1 script file: -File .\Get-Script.ps1 -All

In rare cases, you might need to provide a Boolean value for a switch parameter. To provide a Boolean value for a switch parameter in the value of the File parameter, enclose the parameter name and value in curly braces, such as the following: -File .\Get-Script.ps1 {-All:$False}

-InputFormat {Text | XML}
Describes the format of data sent to Windows PowerShell. Valid values are "Text" (text strings) or "XML" (serialized CLIXML format).

-Mta
Starts Windows PowerShell using a multi-threaded apartment. This parameter is introduced in Windows PowerShell 3.0. In Windows PowerShell 3.0, single-threaded apartment (STA) is the default. In Windows PowerShell 2.0, multi-threaded apartment (MTA) is the default. 

-NoExit
Does not exit after running startup commands.

-NoLogo
Hides the copyright banner at startup.

-NonInteractive
Does not present an interactive prompt to the user.

-NoProfile
Does not load the Windows PowerShell profile.

-OutputFormat {Text | XML}
Determines how output from Windows PowerShell is formatted. Valid values are "Text" (text strings) or "XML" (serialized CLIXML format).

-PSConsoleFile 
Loads the specified Windows PowerShell console file. Enter the path and name of the console file. To create a console file, use the Export-Console cmdlet in Windows PowerShell.

-Sta
Starts Windows PowerShell using a single-threaded apartment. In Windows PowerShell 3.0, single-threaded apartment (STA) is the default. In Windows PowerShell 2.0, multi-threaded apartment (MTA) is the default. 

-Version 
Starts the specified version of Windows PowerShell. The version that you specify must be installed on the system. If Windows PowerShell 3.0 is installed on the computer, valid values are "2.0" and "3.0". The default value is "3.0".

If Windows PowerShell 3.0 is not installed, the only valid value is "2.0". Other values are ignored.

For more information, see "Installing Windows PowerShell" in the Windows PowerShell Getting Started Guide.

-WindowStyle 
Sets the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden.

-Command
Executes the specified commands (and any parameters) as though they were typed at the Windows PowerShell command prompt, and then exits, unless the NoExit parameter is specified. 

The value of Command can be "-", a string. or a script block. If the value of Command is "-", the command text is read from standard input.

Script blocks must be enclosed in braces ({}). You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script are returned to the parent shell as deserialized XML objects, not live objects.

If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments.

To write a string that runs a Windows PowerShell command, use the format:

"& {}"

where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.

-Help, -?, /?
Shows this message. If you are typing a PowerShell.exe command in Windows PowerShell, prepend the command parameters with a hyphen (-), not a forward slash (/). You can use either a hyphen or forward slash in Cmd.exe.

Examples

PowerShell -PSConsoleFile vmwaresnapin.psc1

PowerShell -Version 2.0 -NoLogo -InputFormat text -OutputFormat XML

PowerShell -Command {Get-EventLog -LogName security}

PowerShell -Command "& {Get-EventLog -LogName security}"

# To use the -EncodedCommand parameter:
$command = "dir 'c:\program files' "
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand


What is Windows PowerShell ISE


The Windows PowerShell Integrated Scripting Environment (ISE) is one of two hosts for the Windows PowerShell engine and language.  With it you can write, run, and test scripts in ways that are not available in the Windows PowerShell Console.  The ISE adds syntax-coloring, tab completion, IntelliSense, visual debugging, and context sensitive Help.

The ISE lets you run commands in a console pane, but it also supports panes that you can use to simultaneously view the source code of your script and other tools that can plug into the ISE.  You can even open up multiple script windows at the same time, which is especially helpful when you are debugging a script that uses functions defined in other scripts or modules.


Use any of the following methods to start the Windows PowerShell ISE.


How to Start PowerShell ISE


From the Start Menu:

  • Click Start => type ISE => click Windows PowerShell ISE
    or
  • From the Start menu => click Start => click All Programs => click Accessories => click the Windows PowerShell folder => click Windows PowerShell ISE

At the Command Prompt:

  • In Cmd.exe, Windows PowerShell, or Windows PowerShell ISE, to start Windows PowerShell, type:  PowerShell_ISE or ISE

With Administrative privileges (“Run as administrator”):

  • Click Start => type ISE => right-click Windows PowerShell ISE => click Run as administrator

How to Write and Run Scripts in the PowerShell ISE


  • How to create and run scripts
  • How to write and edit text in the Script Pane
  • How to save a script

How to create a new script file:

You can open and edit Windows PowerShell files in the Script Pane.  Specific file types of interest in Windows PowerShell are script files (.ps1), script data files (.psd1), and script module files (.psm1).  These file types are syntax colored in the Script Pane editor. Other common file types you may open in the Script Pane are configuration files (.ps1xml), XML files, and text files.

The Windows PowerShell execution policy determines whether you can run scripts and load Windows PowerShell profiles and configuration files.  The default execution policy, Restricted, prevents all scripts from running, and prevents loading profiles.  To change the execution policy to allow profiles to load and be used, see Set-ExecutionPolicy and about _signing.

To create a new script file:
On the toolbar, click New , or on the File menu, click New.  The created file appears in a new file tab under the current PowerShell tab. Remember that the PowerShell tabs are only visible when there are more than one.  By default a file of type script (.ps1) is created, but it can be saved with a new name and extension.  Multiple script files can be created in the same PowerShell tab.

To open an existing script:
On the toolbar, click Open…, or on the File menu, click Open.  In the Open dialog box, select the file you want to open. The opened file appears in a new tab.

To close a script tab:
Click the script tab of the script you want to close, then do one of the following:
1.  Click the Close icon (X) on the script tab.
2.  On the File menu, click Close.
If the file has been altered since it was last saved, you are prompted to save or discard it.

For directions on how to save a script, see How to Save a Script .

To display the file path:
On the file tab, point to the file name.  The fully qualified path to the script file appears in a tooltip.

To run a script:
On the toolbar, click Run Script, or on the File menu, click Run.

To run a portion of a script:
1.  In the Script Pane, select a portion of a script.
2.  On the File menu, click Run Selection, or on the toolbar, click Run Selection.

To stop a running script:
On the toolbar, click Stop Operation, press CTRL+BREAK, or on the File menu, click Stop Operation. Pressing CTRL+C also works unless some text is currently selected, in which case CTRL+C maps to the copy function for the selected text.


How to write and edit text in the Script Pane:

Use the following steps to edit text in the Script Pane.  You can copy, cut, paste, find, and replace text.  You can also undo and redo the last action you just performed.  The keyboard shortcuts for performing these actions are the same as those used for all Windows applications.

To enter text in the Script Pane:
1.  Move the cursor to the Script Pane by clicking anywhere in the Script Pane, or by clicking Go to Script Pane in the View menu.
2.  Create a script. Syntax coloring and tab completion make this a richer experience in Windows PowerShell ISE.
3.  See How to Use Tab Completion in the Script Pane and Console Pane for details about using the tab completion feature to help in typing.

To find text in the Script Pane:
1.  To find text anywhere, press CTRL+F or, on the Edit menu, click Find in Script.
2.  To find text after the cursor, press F3 or, on the Edit menu, click Find Next in Script.
3.  To find text before the cursor, press SHIFT+F3 or, on the Edit menu, click Find Previous in Script.

To find and replace text in the Script Pane:
Press CRTL+H or, on the Edit menu, click Replace in Script. Enter both the text you want to find and the text with which you want to replace it, and then press ENTER.

To go to a particular line of text in the Script Pane:
1.  In the Script Pane, press CTRL+G or, on the Edit menu, click Go to Line.
2.  Enter a line number.

To copy text in the Script Pane:
1.  In the Script Pane, select the text that you want to copy.
2.  Press CTRL+C or, on the toolbar, click the Copy icon, or on the Edit menu, click Copy.

To cut text in the Script Pane:
1.  In the Script Pane, select the text that you want to cut.
2.  Press CTRL+X or, on the toolbar, click the Cut icon, or on the Edit menu, click Cut.

To paste text into the Script Pane:
Press CTRL+V or, on the toolbar, click the Paste icon, or on the Edit menu, click Paste.

To undo an action in the Script Pane:
Press CTRL+Z or, on the toolbar, click the Undo icon, or on the Edit menu, click Undo.

To redo an action in the Script Pane:
Press CTRL+Y or, on the toolbar, click the Redo icon, or on the Edit menu, click Redo.


How to save a script:

Use the following steps to save and name a script.  An asterisk appears next to the script name to mark a file that has not been saved since it was altered.  The asterisk disappears when the file is saved.

To save a script:
Press CTRL+S or, on the toolbar, click the Save icon, or on the File menu, click Save.

To save and name a script:
1.  On the File menu, click Save As. The Save As dialog box will appear.
2.  In the File name box, enter a name for the file.
3.  In the Save as type box, select a file type. For example, in the Save as type box, select “PowerShell Scripts (* .ps1)”.
4.  Click Save.

To save a script in ASCII encoding:
By default, Windows PowerShell ISE saves new script files (.ps1), script data files (.psd1), and script module files (.psm1) as Unicode (BigEndianUnicode) by default.  To save a script in another encoding, such as ASCII (ANSI), use the Save or SaveAs methods on the $psISE.CurrentFile object.

The following command saves a new script as MyScript.ps1 with ASCII encoding:
$psise.CurrentFile.SaveAs(“MyScript.ps1”, [System.Text.Encoding]::ASCII)

The following command replaces the current script file with a file with the same name, but with ASCII encoding:
$psise.CurrentFile.Save([System.Text.Encoding]::ASCII)

The following command gets the encoding of the current file:
$psise.CurrentFile.encoding


Windows PowerShell ISE supports the following encoding options: ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, UTF8 and Default.  The value of the Default option varies with the system.

Windows PowerShell ISE does not change the encoding of scripts that were created by in other editors, even when you use the Save or Save As commands in Windows PowerShell ISE.


Facebook Comments