PowerShell


This draft deletes the entire topic.

expand all collapse all

Examples

  • 10

    By default PowerShell is set up, for security reasons, to only allow signed scripts to execute. Executing the following command will allow you to run unsigned scripts (you must run PowerShell as Administrator to do this).

    Set-ExecutionPolicy RemoteSigned
    

    Another way to run PowerShell scripts is to use Bypass as ExecutionPolicy:

    powershell.exe -ExecutionPolicy Bypass -File "c:\MyScript.ps1"
    

    Temporary work-around for execution policy can be achieved by running the Powershell executable and passing any valid policy as -ExecutionPolicy parameter. The policy is in effect only during process' lifetime, so no administrative access to the registry is needed.

    C:\>powershell -ExecutionPolicy RemoteSigned
    

    There are multiple other policies avaialable, and sites online often encourage you to use Set-ExecutionPolicy Unrestricted. This policy stays in place until changed, and lowers the system security stance. This is not advisable. Use of RemoteSigned is recommended, because it allows locally stored and written code, and requires remotely acquired code be signed with certificate from a trusted root.

    TechNet Documentation:
    Set-ExecutionPolicy
    about_Execution_Policies

  • 5

    In PowerShell there are many ways to achieve the same result, this can be illustrated nicely with the simple, familiar Hello World example:

    Using Write-Host cmdlet

    Write-Host "Hello World"  
    

    Using Write-Output cmdlet

    Write-Output "Hello world"
    

    Using Aliases like echo

    echo "Hello world"
    

    Or, simply, typing "Hello world"

    "Hello world"
    

    All of which will result with the expected console output

    Hello World
    

    It's worth noting however that while these all achieve the goal "write to screen", Write-Host and Write-Output differ in that the former prints only to stdout (i.e. the console screen) whereas the latter produces an object which can be captured in a variable or pipeline.

    echo and write are aliases for Write-Output.

  • 2

    One of the first questions people have when they begin to use PowerShell for scripting is how to manipulate the output from a cmdlet to perform another action.

    The pipeline symbol | is used at the end of a cmdlet to take the data it exports and feed it to the next cmdlet. A simple example is using Select-Object to only show the Name property of a file shown from Get-ChildItem:

    Get-ChildItem | Select-Object Name
    #This may be shortened to:
    gci | Select Name
    

    More advanced usage of the pipeline allows us to pipe the output of a cmdlet into a foreach loop:

    Get-ChildItem | ForEach-Object {
        Copy-Item -Path $_.FullName -destination C:\NewDirectory\ 
    }
    

    Note that the example above uses the $_ symbol - when objects from the pipeline aren't specifically named using | foreach($item in $items) for example, they use this variable.

Please consider making a request to improve this example.

Remarks

Windows PowerShell is an automation/configuration management framework from Microsoft built off of the .NET Framework. PowerShell is installed by default on all supported versions of Windows client and server operating systems since Windows 7 / Windows Server 2008 R2. Powershell can be updated at any time by downloading a later version of the Windows Management Framework (WMF). The "Alpha" version of PowerShell 6 is cross-platform (Windows, Linux, and OS X) and needs to be downloaded and installed from this release page.

Additional resources:

Versions

VersionIncluded with WindowsRelease Date
1.0XP / Server 20082006-11-01
2.07 / Server 2008 R22009-11-01
3.08 / Server 20122012-08-01
4.08.1 / Server 2012 R22013-11-01
5.010 / Server 2016 Tech Preview2015-12-16
5.1 Preview10 Anniversary edition / Server 20162016-07-16

Note: v5.1 will be GA when Server 2016 is officially released, but the Preview is part of Win 10 Anniversary edition - MS blog post.

Still have a question about Getting started with PowerShell? Ask Question

Topic Outline