PowerShell

PowerShell TutorialAnonymize IP (v4 and v6) in text file with PowershellGUI in PowershellHow to download latest artifact from Artifactory using Powershell script (v2.0 or below)PowerShell ActiveDirectory modulePowerShell AliasesPowerShell Amazon Web Services (AWS) RekognitionPowerShell Amazon Web Services (AWS) Simple Storage Service (S3)PowerShell Archive ModulePowerShell Automatic VariablesPowerShell Automatic Variables - part 2PowerShell Background JobsPowerShell Basic Set OperationsPowerShell Built-in variablesPowerShell Calculated PropertiesPowerShell ClassesPowerShell Cmdlet NamingPowerShell Comment-based helpPowerShell Common parametersPowerShell Communicating with RESTful APIsPowerShell Conditional logicPowerShell Creating DSC Class-Based ResourcesPowerShell CSV parsingPowerShell Desired State ConfigurationPowerShell Dynamic ParametersPowerShell Embedding Managed Code (C Sharp Or VB)PowerShell Enforcing script prerequisitesPowerShell Environment VariablesPowerShell Error handlingPowerShell FunctionsPowerShell Handling Secrets and CredentialsPowerShell HashTablesPowerShell Infrastructure AutomationPowerShell Introduction to PesterPowerShell Introduction to PsakePowerShell ISE modulePowerShell LoopsPowershell ModulesPowerShell Modules, Scripts and FunctionsPowerShell MongoDBPowerShell Naming ConventionsPowerShell OperatorsPowerShell Package managementPowerShell Parameter setsPowerShell PowerShell.exe Command-LinePowershell profilesPowerShell Regular ExpressionsPowershell RemotingPowerShell Running ExecutablesPowerShell Scheduled tasks modulePowerShell Security and CryptographyPowerShell Sending EmailPowerShell SharePoint ModulePowerShell Signing ScriptsPowerShell Special OperatorsPowerShell SplattingPowershell SQL queriesPowerShell Streams; Debug, Verbose, Warning, Error, Output and InformationPowerShell StringsPowerShell Switch statementPowerShell URL Encode/DecodePowerShell Using existing static classesPowerShell Using ShouldProcessPowerShell Using the Help SystemPowerShell Using the progress barPowerShell WMI and CIMPowerShell WorkflowsPowerShell Working with ObjectsPowerShell Working with XML FilesPSScriptAnalyzer - PowerShell Script AnalyzerReturn behavior in PowerShellTCP Communication with PowerShellVariables in PowerShellWorking with the PowerShell pipeline



PowerShell Tutorial

From WikiOD

Windows PowerShell is a shell and scripting component of the Windows Management Framework, an automation/configuration management framework from Microsoft built on 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[edit | edit source]

Version Included with Windows Notes Release Date
1.0 XP / Server 2008 2006-11-01
2.0 7 / Server 2008 R2 2009-11-01
3.0 8 / Server 2012 2012-08-01
4.0 8.1 / Server 2012 R2 2013-11-01
5.0 10 / Server 2016 Tech Preview 2015-12-16
5.1 10 Anniversary edition / Server 2016 2017-01-27

Allow scripts stored on your machine to run un-signed[edit | edit source]

For security reasons, PowerShell is set up by default 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"

Or from within your existing PowerShell console or ISE session by running:

 Set-ExecutionPolicy Bypass Process

A temporary workaround for execution policy can also 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 available, 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 a certificate from a trusted root.

Also, beware that the Execution Policy may be enforced by Group Policy, so that even if the policy is changed to Unrestricted system-wide, Group Policy may revert that setting at its next enforcement interval (typically 15 minutes). You can see the execution policy set at the various scopes using Get-ExecutionPolicy -List

TechNet Documentation:

Set-ExecutionPolicy

about_Execution_Policies

Aliases & Similar Functions[edit | edit source]

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:

Write*Host "Hello World"

Using Write-Output:

Write*Output 'Hello world'

It's worth noting that although Write-Output & Write-Host both write to the screen there is a subtle difference. Write-Host writes only to stdout (i.e. the console screen), whereas Write-Output writes to both stdout AND to the output [success] stream allowing for redirection. Redirection (and streams in general) allow for the output of one command to be directed as input to another including assignment to a variable.

> $message = Write-Output "Hello World"
> $message
"Hello World"

These similar functions are not aliases, but can produce the same results if one wants to avoid "polluting" the success stream.

Write*Output is aliased to Echo or Write

Echo 'Hello world'
Write 'Hello world'

Or, by simply typing 'Hello world'!

'Hello world'

All of which will result with the expected console output

Hello world

Another example of aliases in PowerShell is the common mapping of both older command prompt commands and BASH commands to PowerShell cmdlets. All of the following produce a directory listing of the current directory.

C:\Windows> dir
C:\Windows> ls
C:\Windows> Get-ChildItem

Finally, you can create your own alias with the Set-Alias cmdlet! As an example let's alisas Test-NetConnection, which is essentially the PowerShell equivalent to the command prompt's ping command, to "ping".

Set*Alias -Name ping -Value Test-NetConnection

Now you can use ping instead of Test-NetConnection! Be aware that if the alias is already in use, you'll overwrite the association.

The Alias will be alive, till the session is active. Once you close the session and try to run the alias which you have created in your last session, it will not work. To overcome this issue, you can import all your aliases from an excel into your session once, before starting your work.

The Pipeline - Using Output from a PowerShell cmdlet[edit | edit source]

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\ 
}

#This may be shortened to:
gci | % { Copy $_.FullName C:\NewDirectory\ }

Note that the example above uses the $_ automatic variable. $_ is the short alias of $PSItem which is an automatic variable which contains the current item in the pipeline.

Calling .Net Library Methods[edit | edit source]

Static .Net library methods can be called from PowerShell by encapsulating the full class name in third bracket and then calling the method using ::

#calling Path.GetFileName()
C:\> [System.IO.Path]::GetFileName('C:\Windows\explorer.exe')
explorer.exe

Static methods can be called from the class itself, but calling non-static methods requires an instance of the .Net class (an object).

For example, the AddHours method cannot be called from the System.DateTime class itself. It requires an instance of the class :

C:\> [System.DateTime]::AddHours(15)
Method invocation failed because [System.DateTime] does not contain a method named 'AddHours'.
At line:1 char:1
+ [System.DateTime]::AddHours(15)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

In this case, we first create an object, for example :

C:\> $Object = [System.DateTime]::Now

Then, we can use methods of that object, even methods which cannot be called directly from the System.DateTime class, like the AddHours method :

C:\> $Object.AddHours(15)

Monday 12 September 2016 01:51:19

Installation or Setup[edit | edit source]

Windows[edit | edit source]

PowerShell is included with the Windows Management Framework. Installation and Setup are not required on modern versions of Windows.

Updates to PowerShell can be accomplished by installing a newer version of the Windows Management Framework.

Other Platforms[edit | edit source]

"Beta" version of PowerShell 6 can be installed on other platforms. The installation packages are available here.

For example, PowerShell 6, for Ubuntu 16.04, is published to package repositories for easy installation (and updates).

To install run the following:

# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

# Register the Microsoft Ubuntu repository
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list

# Update apt-get
sudo apt-get update

# Install PowerShell
sudo apt-get install -y powershell

# Start PowerShell
powershell

After registering the Microsoft repository once as superuser, from then on, you just need to use sudo apt-get upgrade powershell to update it. Then just run powershell

Commenting[edit | edit source]

To comment on power scripts by prepending the line using the # (hash) symbol

# This is a comment in powershell
Get-ChildItem

You can also have multi-line comments using <# and #> at the beginning and end of the comment respectively.

<#
This is a 
multi-line
comment
#>
Get-ChildItem

Creating Objects[edit | edit source]

The New-Object cmdlet is used to create an object.

# Create a DateTime object and stores the object in variable "$var"
$var = New-Object System.DateTime

# calling constructor with parameters
$sr = New-Object System.IO.StreamReader -ArgumentList "file path"

In many instances, a new object will be created in order to export data or pass it to another commandlet. This can be done like so:

$newObject = New-Object -TypeName PSObject -Property @{
    ComputerName = "SERVER1"
    Role = "Interface"
    Environment = "Production"
}

There are many ways of creating an object. The following method is probably the shortest and fastest way to create a PSCustomObject:

$newObject = [PSCustomObject]@{
    ComputerName = 'SERVER1'
    Role         = 'Interface'
    Environment  = 'Production'
}

In case you already have an object, but you only need one or two extra properties, you can simply add that property by using Select-Object:

Get*ChildItem | Select-Object FullName, Name, 
    @{Name='DateTime'; Expression={Get-Date}}, 
    @{Name='PropertieName'; Expression={'CustomValue'}}

All objects can be stored in variables or passed into the pipeline. You could also add these objects to a collection and then show the results at the end.

Collections of objects work well with Export-CSV (and Import-CSV). Each line of the CSV is an object, each column a property.

Format commands convert objects into text stream for display. Avoid using Format-* commands until the final step of any data processing, to maintain the usability of the objects.

Credit:Stack_Overflow_Documentation