Designed by |
Jeffrey Snover, Bruce Payette, James Truher (et al.) |
Developer |
Microsoft |
First appeared |
November 14, 2006 |
Stable release |
5.1.14393 / August 2, 2016; 8 months ago |
Preview release |
6.0.0 Alpha 17 / March 8, 2017; 35 days ago |
Typing discipline |
Strong, safe, implicit and dynamic |
Platform |
.NET Framework, .NET Core |
OS |
Windows 7 and later, macOS, CentOS, Ubuntu |
Filename extensions |
•.ps1 (Script) •.ps1xml (XML Document) •.psc1 (Console File) •.psd1 (Data File) •.psm1 (Script Module) •.pssc (Session Configuration File) •.cdxml (Cmdlet Definition XML Document) |
Easy to find out any way, just clue is required. (Get-Command "*Service*" )
Install an MSI package on a remote computer:(Get-WMIObject -ComputerName TARGETMACHINE -List | Where-Object -FilterScript{$_.Name-eq "Win32_Product"}).Install(\\MACHINEWHEREMSIRESIDES\path\package.msi)
Upgrade an installed application with an MSI-based application upgrade package:
(Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='name_of_app_to_be_upgraded'").Upgrade(\\MACHINEWHEREMSIRESIDES\path\upgrade_package.msi)
Remove an MSI package from the current computer:(Get-WmiObject-ClassWin32_Product-Filter "Name='product_to_remove'" -ComputerName . ).Uninstall()
Stop-Computer “ComputerName1, Name2”Stop-Computer –computer DC1 –Credential nwtraders\administrator
Shutdown –i (to restart/shutdown bulk of computer in one shot with message)
Restart-Computer “ComputerName1”
Get-wmiobject –class WIN32_OperatingSystem –ComputerName .).Invokemethod(“Win32Shutdown”,0)Or(gwmi Win32_OperatingSystem).Win32Shutdown(0).
IF / IF-Else
$var = (get-process).countif($var -gt 110)
{
Write-Host "We have $var process in running state "
}
Else {
Write-Host “Number of Processes are less than 100 in count.”
Read User Input/Display Output
Write-Host is having more attributes
Write-Host –NoNewLine “Counting from 1 to 9 (in seconds): “
foreach($element in 1..9){
Write-Host –NoNewLine “${element} “
Start-Sleep –Seconds 1
}
Write-Host “”
Output :- Counting from 1 to 9 (in seconds): 1 2 3 4 5 6 7 8 9
Write-Output should be used when you want to send data on in the pipe line, but not necessarily want to display it on screen.
PS C:\> Write-Output"test output" | Get-Member
This command pipes the "test output" string to the Get-Member cmdlet, which displays the members of the System.String class, demonstrating that the string was passed along the pipeline.
For / For-Each Loop
ForEach is specially used to fetch elements from an array
While/ Do-While Loop
While :- As long as the condition remains true, PowerShell reruns the {command_block} section.
while($val -ne 10) { $val++ Write-Host $val }
Do-While:- First the Command Block will run and then it will check condition
Do {
$val++ Write-Host $val
} while($val -ne 10)
Importing:
Get-Content :- To Read content from a file (.txt/ .csv /.xlsx)
$var = Get-Content -Path .\Desktop\names.txt
foreach ($i in $var)
{
Write-Host $i
Start-Sleep -Seconds 1
}
Import-csv :- To Read Data from csv file $var = Import-Csv -Path .\Desktop\names.csv