PowerShell

1) Download Window Management Framework v5
2) get-alias will list all shortcuts
3) update-help -force : this will update help from internet
4) get-help *process* : it will find all the command which has process in it
5) get-help get-process : it will give more information about commands
6) get-help *ipaddress* : To find all commands which has ipaddress in it.
7) get-help get-verb : It will show all the verbs
8) get-help get-process -Detailed : This will give you more detail information about all parameters
9) get-help get-process -Examples: This will only show examples for that command
10)get-help get-prcess -full : This will show what are the parameters are default in command.
11) get-help get-process -showWindow : it will show help in new window
12)get-module: What modules are currently loaded in my enviornment
13)get-module -listavailable: it will list the modules which are availale in my machine but not in workspace
14) import-module -name ActiveDirectory: THis is the old way to import module from my machine to my enviornment
15)get-command -Module ActiveDirectory: This will give all the available commands from ActiveDirectory module
New way to import module is:
16) it will dynamically add module when we run command from that module
To install module from remote repository
17) find-module : return list of available module in microsoft repo(default)
18) install-module <name from above list>: It will install module in our workspace
19) imoport-module PSReadline
20) set-PSReadlineoption -EditMode Emacs: This command will show different options are availalbe when we press TAB
21)find-package: If we want to change the default remote repo from microsoft to something else then we have to use this command
22) get-service | stop-service -WhatIf : This will just tell us what would have happened if we execute find-package | stop-service command
23) get-service | out-file -filepath c:service.txt: it will copy the output of get-service into the file
24) get-content -path c:service.txt : to see the date in file. we can also use cat c:service.txt to do the same
25)get-eventlog -Logname System : to see windows log
26) get-eventlog -Logname security -EntryType error -Newest 5 : only want to see 5 new error logs
27) get-eventlog -Logname system -EntryType error -Newest 5 | out-file c:error.txt
28) get-service | export-csv -path c:\servcice.csv | if you want to manuapulate or update the data then use convertto-csv else use export-csv
29) import-service -path c:\servcice.csv
30) get-service | export-csv  -path c:/service.csv -NoTypeInformation : This file is supported for third type application
31) get-service | convertto-csv -NoTypeInformation | out-file c:\test.csv : we can do lot of manuapulation in converto-csv
32) get-process | export-clixml -path c:/process.xml : This is helpful to to send info to database
32) import-clixml c:/process.xml : looks just like when we ran get-process
34) get-process | export-clixml c:/gold.xml
Example: we are comparing the number of process in two  machine. We run above command for reference/good machine and below command in machine where we had some issue to see what are the process it’s running.
compare-object -referenceobject (import-clixml c:\gold.xml) -DifferenceObject (get-process) -Property Processname:  : we have used parenthesis so that powershell will import this file for reference when it compares
35) get-eventlog -Logname system -Newest 5 -EntryType error | Converto-HTML | out-file c:\error.html : it will create html file for us
36) get-eventlog -Logname system -Newest 5 -EntryType error | Converto-HTML -Title “Windows Errors” -Body (Get-Date) -PreConetent “<P> Generated by IT </p>” -Postconetent “For more details check the full server log” | out-file c:\error.html
37)get-service | get-member or get-servier | gm : This will show us what are the method and proprts available for command.
get-service | gm : you will see lot of proporty and method when you execute this command if you want to exact specific property then execute below command
38) get-service -name bits | select-object -Property Name,Status,MachineName : Name, Status and machineName are different property of get-service. We would like to just select those three propty to disaplay when we execute get-service command
39) how to sort the information based upon specific properties?
get-service | select-object -property name, status | sort-object -property status : this will sort based upon status property
40)get-childitem -path c:/ | sort-object -property length -Descending  | select name, length
To create own properties:
In below example we have created our own properties name called as servicename and assinged value Hellow as below,

PS C:\Users\Administrator> Get-Service -name bits | Select-Object -Property Name, @{name=”ServiceName”;expression={“Hell
o”}}

Name ServiceName
—- ———–
bits Hello

In below example, I am creating my own propery “Servicename” and value that pasing to the propery is name. Therefore whatever value present under propery name will be seen under ServiceName.
$_ This indicate the current objects present in memory.
In short we have created custom name and status

PS C:\Users\Administrator> Get-Service -name bits | Select-Object @{name=”ServiceName”;expression={$_.name}}

ServiceName
———–
bits

Comparison operator in powershell:
4 -gt 3 : true
4 -lt 3 : false
4 -eq 3 : flase
4 -ne 3 : True
4 -le 3 : true

“Hello” -eq “Hello” : True
“Hello” -eq “HELLO” : True , Window it’s not case sensetive by default
“Hello” -ceq “HELLO” : False, c stand for case sensetive

Filter-object: In this object we have to use comparison operator
Get-service | where-object {$_.status -eq “Stopped”}

This will show all stopped services

Working with Methods:
get-service -name bits | foreach-object {$_.status()}

Policy to run script:
Get-ExecutionPolicy: if the output is restricted that means we can’t execute any scripts
set-execitionpolicy remotesigned: we can execute scripts if we create or download it from internet if it’s singed and approved

Lets start write your first script:
Copy below commands in notepad and save file with extension “.ps1”.
get-eventlog -Logname system -Newest 5 -EntryType error -computername dc, s1, s2| select-object -property index,source, message | converto-html | out-file c:/error.html

write-output : this is use to print anything on screen

$test = 1,2,3,4 : this is variable as an array
$test[2] : 10 : we have updated 2nd location with value of 10

Try to learn all script in “ise”. it’s free software and integrated with powersheel. Just type ise to launch

get-ciinstance -classname Win32_localdisk

if we save scrip name as test.ps1 and when we try to execute CM, by default it will take google.com as input. However, we can pass same parameter as well as shown below
param(
$domainname=”google.com”
)
Test-Connection $domainname -Quiet

.\test.ps1 -domainname amazon.com: this will use amazon.com in above script