Getting Started with Windows PowerShell

line5

Firstly you have to Install PowerShell

In this lab scenario Window 8.1 operating system is used. Since windows 8.1 comes with pre-installed PowerShell you can skip this step. For earlier versions such as Windows Vista you need to download and install PowerShell.

Download Link: https://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

To Check the PowerShell version

get-host

get-host.JPG

get-host |select-object version 

get-host version

$PSVersionTable

versiontable.JPG

Set PowerShell’s Execution Policy

  • Restricted – PowerShell won’t run any scripts. This is PowerShell’s default execution policy.
  • AllSigned – PowerShell will only run scripts that are signed with a digital signature. If you run a script signed by a publisher PowerShell hasn’t seen before, PowerShell will ask whether you trust the script’s publisher.
  • RemoteSigned – PowerShell won’t run scripts downloaded from the Internet unless they have a digital signature, but scripts not downloaded from the Internet will run without prompting. If a script has a digital signature, PowerShell will prompt you before it runs a script from a publisher it hasn’t seen before.
  • Unrestricted – PowerShell ignores digital signatures but will still prompt you before running a script downloaded from the Internet.
  • Bypass – Everything runs without a warning. Be careful with this one.
  • Undefined – No policy is defined in the current scope. This is used to allow fall-back to policies defined in lower scopes (more details below) or to the OS defaults.

Check the current policy

Get-ExecutionPolicy

01 get policy

Change the policy into RemoteSigned

Set-ExecutionPolicy RemoteSigned

02 set policy

Run Your First PowerShell script

Create a script file with following content. Name the file as MyScript.ps1

Write-Host "Hello, world"

Save the MyScript.ps1 file into following folder

C:\Scripts

03 capture

Run Following commands at PowerShell

PS C:\> Set-Location C:\Scripts

PS C:\Scripts> .\MyScript.ps1

04 run script

Alternatively you can use cmd.exe to run the scripts

In this case following script is used to get the information.

computer hostname = w

$computers = "w"

Get-WmiObject -Class win32_bios -cn $computers |

Format-table __Server, Manufacturer, Version -AutoSize

05 cmd prompt

You can use PowerShell scripts against Multiple Computers

In this scenario Windows Server 2012 R2 server domain environment is used to run the scripts.

Save following script in MyScript.ps1 file.

$computers = Get-Content -Path C:\Scripts\Computers.txt

Get-WmiObject -Class win32_bios -cn $computers -EA silentlyContinue |

Format-table __Server, Manufacturer, Version –AutoSize

Watermark Multiple Computers MyScript script file

And Make sure the computers list in the network is saved as a text file in Computers.txt.

win7
S2012

Computers list Text File.png

The path of the text file should be

C:\Scripts\Computers.txt

Run the PowerShell script

PS C:\> Set-Location C:\Scripts

PS C:\Scripts> .\MyScript.ps1

Watermark Multiple Computers powershell

REFERENCE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s