Week 14 15 PowerShell 1
Week 14 15 PowerShell 1
PowerShell
Shell
PowerShell is modern command shell that includes the best features of other popular shells. Unlike most shells that only accept and return text, PowerShell accepts and returns
.NET objects. The shell includes the following features:
• Robust command-line history
• Tab completion and command prediction (See about_PSReadLine)
• Supports command and parameter aliases
• Pipeline for chaining commands
• In-console help system, similar to Unix man pages
Scripting language
As a scripting language, PowerShell is commonly used for automating the management of systems. It is also used to build, test, and deploy solutions, often in CI/CD
environments. PowerShell is built on the .NET Common Language Runtime (CLR). All inputs and outputs are .NET objects. No need to parse text output to extract information
from output. The PowerShell scripting language includes the following features:
• Extensible through functions, classes, scripts, and modules
• Extensible formatting system for easy output
• Extensible type system for creating dynamic types
• Built-in support for common data formats like CSV, JSON, and XML
Configuration management
PowerShell Desired State Configuration (DSC) is a management framework in PowerShell that enables you to manage your enterprise infrastructure with configuration as code.
With DSC, you can:
• Create declarative configurations and custom scripts for repeatable deployments
• Enforce configuration settings and report on configuration drift
• Deploy configuration using push or pull models
WHAT IS WINDOWS POWERSHELL ?
WHY POWERSHELL ?
• It’s not going away any time soon ( Microsoft has made it clear that PowerShell is here to stay. That’s why we have PowerShell version 2 included in Windows
Server 2008 R2 and Windows 7 by default.)
• Most Microsoft products will eventually use it
• You can’t do everything from the GUI any more
• It can make your life easier( Example: If you need to update an Active Directory attribute for a thousand users. Performing the task manually would likely take
hours to complete. Using PowerShell, you can complete the task using a single line of code.)
• Many GUIs are PowerShell front ends ( best known example of this is the Exchange Management Console )
• You can use PowerShell commands to manage your domains
• It enables interactivity between products
HOW ITS DIFFERS ?
POWERSHELL APPLICATION
Windows PowerShell Console
• Similar to the Command Prompt
• Launching Windows PowerShell Console.
• Go to Start -> All Programs -> Accessories -> windows PowerShell -> Windows PowerShell
• You can also launch PowerShell from a command prompt start -> run by simply typing PowerShell
• Writing PowerShell script *.PS1 and executing through PowerShell console.
• Launching Windows PowerShell Console.
• Go to Start -> All Programs -> Accessories -> windows PowerShell -> Windows PowerShell ISE
• You can also launch PowerShell from a command prompt start -> run by simply typing PowerShell ISE
POWERSHELL CMDLETS
Cmdlets are compiled code that are available to the PowerShell environment. PowerShell commands have been standardized using a "verb-noun" naming convention.
This standard simplifies the learning curve and provides a better description of what the cmdlet does
To see a list of cmdlets available in PowerShell, type the following cmdlet:
Get-Command (Get is your verb and Command is your noun)
Get-Command –Verb Get: List all the commands which has verb “Get”.
Get-Command –Noun Service: List all the commands which has noun “Service”.
It is important to find information quickly and easily. Get-Help cmdlet has been designed for that purpose. It displays help about Windows PowerShell
cmdlets and concepts.
Get-Help: Information about cmdlets and concepts. Includes description, syntax, and remarks
Get-Help *: Information about all available help topics.
Get-Help Get-Service: Information about a specific cmdlet.
Get-Help Get-Service –Example: Information about a specific cmdlet with examples
USING CMDLETS
Get-Process: List all the process running in the machine Get-Service: List all the service running in the machine Get-Location: Gets the current path
Get-Service | Get-Member: List all the properties and methods of the Get-Service cmdlets
Get-Service | Get-Member -MemberType Method: List only the methods
Get-Service | Get-Member –MemberType Property: List only the property
FORMATTING OUTPUT:
The “Format-” cmdlets allow us to choose which format to display results in.
• Format-List à Displays the data in list
• Format-Table à Displays the data in rows and column
• Format-Wide à Compresses results of a list, to fit the screen
Get-Process | ConvertTo-html: Convert the output to html, Display the result in PowerShell console.
Get-Process | ConvertTo-html | out-file “C:\Processes.html”: Saves the output to a file.
Get-Process | Export-CSV Processes.csv: Convert the output to Process.csv file.
Invoke-Item Processes.csv: Open the exported Process.csv file.
POWERSHELL PROVIDERS
PowerShell Providers are .NET programs that allow us to work with data stores as if they were mounted drives.Simplifies accessing external data outside
the PowerShell environment. For example, we can access the registry as if it were a file system.
To use any of the above listed providers , First we need to connect the providers by mounting PowerShell Drive.
Most Providers have only one PSDrive, the exceptions are the FileSystem Provider(depends on the number of drives on the system) and the Registry
Provider(HKLM and HKCU).
Get-PSDrive: List the available powershell drivers.
Operators:
If Else Switch
$x = 2 $objWMI = Get-WmiObject -Class win32_ComputerSystem -namespace
}
POWERSHELL FUNCTION:
Function Time {Get-Date}
Function Add
{
$args[0] + $args[1]
}
allows us to join two or more statements with a pipe symbol, sometimes called the '|' bar key. This tells PowerShell that we want to take the output of
one command and pass it as the input to the next command. Let’s look at a simple example to better understand the power of Pipeline in Windows
PowerShell.