Module 1
Module 1
Learning PowerShell can be very valuable, especially if you work in a Windows environment
or with Microsoft products. PowerShell is a powerful scripting language and command-line
shell developed by Microsoft specifically for system administration. It allows you to
automate tasks, manage systems, and access various system components using commands
and scripts.
Powershell is worth learning for windows users. Linux users can also learn powershell but
they already have powerful bash shell which they can use. But for windows user before
powershell the only command line shell available was CMD. In fact for over two decades
CMD was the only option for windows users. But CMD is old school. And it is also not as
powerful as powershell.
VBScript is good for automation but it has two major shortcomings. One that Microsoft never
paid attention to its development. Several of windows feature are not supported in VBScript
because Microsoft did not develop it. And the other shortcoming was that VBScript is a pure
developer tool. So if you are not from developer background then you may find it difficult to
understand.
While powershell is easy to learn and you can literally manage everything in system using
powershell. So powershell is best option among CMD, VBScript and powershell.
Apart from this, powershell is a very powerful object based interactive shell. And it is highly
efficient as scripting tool also. In fact powershell is now used very commonly for scripting in
production environment. Wherever you go, if you are in windows environment you will find
powershell in use.
Powershell is very easy to learn. You can leverage powershell to automate .Net objects, COM
objects, WMI, XML or active directory. You can read excel file with powershell and create
beautiful HTML reports. In fact we can use powershell for ethical hacking and penetration
testing as well.
Since powershell is object based so output generation and formatting in powershell is much
easier and efficient than linux bash shell also.
If you are a windows user then you cannot afford to ignore powershell. If you are working in
IT industry and you know powershell then rest assured that you are having an edge over
others.
PowerShell ISE
The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for
Windows PowerShell. In Windows PowerShell ISE, you can run commands and write, test,
and debug scripts in a single Windows-based graphic user interface with multiline editing, tab
completion, syntax coloring, selective execution, context-sensitive help, and support for
right-to-left languages.
You can use menu items and keyboard shortcuts to perform many of the same tasks that you
would perform in the Windows PowerShell console. For example, when you debug a script in
the Windows PowerShell ISE, to set a line breakpoint in a script, right-click the line of code,
and then click Toggle Breakpoint.
PowerShell commands-
28/08/2024
BASIC COMMANDS
Help Files-
Help files in PowerShell are documentation resources that provide detailed information about
cmdlets, functions, scripts, and other elements within the PowerShell environment. These
files explain what each command does, how to use it, what parameters it accepts, examples of
usage, and more.
1. Cmdlet Descriptions:
o Purpose: A summary of what the cmdlet or function does.
o Syntax: The various ways you can use the cmdlet, showing all possible
parameters and their combinations.
o Parameters: Detailed information about each parameter, including its type,
whether it’s required or optional, and any default values.
o Examples: Practical examples demonstrating how to use the cmdlet in
different scenarios.
o Inputs and Outputs: Information on what type of input the cmdlet accepts
and what type of output it produces.
o Related Links: Links to related cmdlets or topics for further reading.
2. About Topics:
o Purpose: Provides information on broader topics, such as PowerShell
concepts (e.g., pipelines, error handling), best practices, or overviews of how
to use PowerShell in specific contexts (e.g., working with objects, scripting
basics).
o Structure: These are more like articles or chapters in a manual, providing
context and guidance beyond just individual cmdlet usage.
3. Glossary:
o Purpose: Definitions of key terms and concepts used in PowerShell.
o Utility: Useful for users new to PowerShell or needing clarification on
specific terminology.
Using Get-Help:
o You can access help for any cmdlet directly within PowerShell by using the
Get-Help cmdlet.
o Example:
Get-Help Get-Process
This command retrieves the help information for the Get-Process cmdlet.
o -Full: Provides the complete help file, including syntax, detailed descriptions,
parameter info, examples, and more.
Viewing Examples:
o You can directly view just the examples using:
Online Help:
o If the local help files are not available, or if you want the most up-to-date
information, you can use:
o This opens the official online documentation in your default web browser.
Purpose and Importance:
Learning Tool: Help files are essential for learning how to use PowerShell
effectively, offering step-by-step guidance and explanations.
Reference: They serve as a quick reference for syntax and usage, particularly when
dealing with complex cmdlets or unfamiliar modules.
Offline Access: Once downloaded, help files can be accessed even when you don't
have an internet connection.
Help files are a core resource for anyone using PowerShell, from beginners to advanced
users, providing the necessary documentation to use PowerShell commands effectively.
1. Update-help –
The Update-Help cmdlet is used to download and install the latest help files directly to your
computer from the internet. If you want to update all help files, simply run:
Update-Help
This will download the most recent help files from Microsoft's servers and install them
locally, so you can access them even when you're offline.
If you want to view the most up-to-date help files online without downloading them, you can
use the -Online parameter with Get-Help. This will open the relevant help page in your
default web browser.
This command will open the official Microsoft documentation page for the Get-Process
cmdlet in your web browser.
Update-Help
Network Issues: Ensure your internet connection is stable when running Update-
Help.
Administrative Rights: You may need to run PowerShell as an administrator to
update help files for all users.
By using the Update-Help cmdlet, you can keep your local help files up to date, ensuring that
you have access to the latest documentation even when you're offline. Meanwhile, the Get-
Help -Online command allows you to quickly access the most recent online documentation.
Example:
Get-Help Start-Service -Detailed
Summary:
3. Get-Process- gets details of all processes running in your local or remote system.
4. Get-Host – to get the latest version of powershell you are using
5. Get-Command- The Get-Command cmdlet in PowerShell is used to retrieve
information about cmdlets, functions, workflows, aliases, and scripts available in the
current session. It returns CmdletInfo Object.
6. (Get-Command).count – counts the number of commands available in the current
session.
7. Get-Command *service* - to get service related cmdlets in the current session.
8. (Get-Command *service*).count- to count the service service related cmdlets in the
current session.
9. Get-Command *-service* - commands having “-service” as the keyword
10. Get-Help Get-Service – to get help on how to use Get-Service command
11. Get-Command *service* | Select-Object CommandType, Name, Version,
ModuleName – to display module specific commandlets
a. CommandType: Indicates the type of command (e.g., Cmdlet, Function,
Application).
b. Name: The name of the command.
c. Version: The version of the command.
d. ModuleName: The name of the module from which the command originates.
This field will be empty for applications since they are not part of any
PowerShell module.
29/08/2024
Common Aliases:
05/09/2024
NETWORK CONFIGURATION
Configure network settings on Windows Server using Windows PowerShell
1. Get-NetIPAddress –
2. Get-NetIPConfiguration
Get-NetIPConfiguration
Get-NetIPConfiguration -All
This command gets the IP configuration information for all of the interfaces
on the computer, including virtual interfaces, loopback interfaces, and
disconnected interfaces.
Get-NetIPConfiguration -InterfaceIndex 12
This command gets the IP configuration information for the interface at the
index 12.
Get-NetIPConfiguration | Get-NetIPAddress
This command gets the IP address information, including the prefix length.
3. Test-NetConnection
The Test-NetConnection cmdlet displays diagnostic information for a
connection. It supports ping test, TCP test, route tracing, and route selection
diagnostics. Depending on the input parameters, the output can include the
DNS lookup results, a list of IP interfaces, IPsec rules, route/source address
selection results, and/or confirmation of connection establishment.
Example 2:
Test-NetConnection www.microsoft.com
The command tests the network connectivity to www.microsoft.com by
resolving its IP address and performing a ping to check if it is reachable.
Example 3:
Test-NetConnection -ComputerName www.microsoft.com -
InformationLevel Detailed
The command performs a comprehensive network connectivity test to
www.microsoft.com and provides detailed diagnostic information,
including results of the ping test, DNS resolution, and additional network-
related data.
Example 4:
Test-NetConnection -ComputerName www.microsoft.com | Select –
ExpandProperty PingReplyDetails | FT Address, Status,
RoundTripTime
The command tests the network connectivity to `www.microsoft.com`,
extracts detailed ping reply information, and displays the address, status, and
round-trip time in a formatted table.
4. Resolve-DnsName
Resolve-DnsName is a command (cmdlet) in PowerShell, which is used to
query and resolve DNS (Domain Name System) names. It allows you to look
up DNS records for a given domain name or IP address, similar to the
nslookup tool.
09/09/2024
PIPELINE
Pipelines act like a series of connected segments of pipe. Items moving along the pipeline
pass through each segment. To create a pipeline in PowerShell, you connect commands
together with the pipe operator "|". The output of each command is used as input to the next
command. The notation used for pipelines is similar to the notation used in other shells.
Each command in a pipeline (called a pipeline element) passes its output to the next
command in the pipeline, item-by-item. Commands don't have to handle more than one item
at a time. The result is reduced resource consumption and the ability to begin getting the
output immediately
1. SELECTING OBJECT
get-process | select-object id, processname
You can use the Select-Object cmdlet to create new, custom Windows PowerShell objects
that contain properties selected from the objects you use to create them.
2. SORTING OBJECT
get-process | sort-object id
We can organise displayed data to make it easier to scan by using the Sort-Object cmdlet.
Sort-Object takes the name of one or more properties to sort on and returns data sorted by the
values of those properties
3. FILTERING OBJECT
The key to making the most of Windows PowerShell is to understand how objects and the
pipeline work together to let you retrieve exactly the information you need. Each command in
the pipeline generates one or more objects and passes them down the pipeline to the next
command. Integral to this process is the ability to filter the objects and their data as they pass
from one command to the next. To that end, PowerShell includes a number of cmdlets that, in
one way or another, let you create commands that refine your pipeline's output.
Where-Object is one of the most commonly used cmdlets in PowerShell and probably the one
most often used for filtering data. This cmdlet filters objects passed down the pipeline based
on the search criteria you specify in a script block included with the cmdlet. In other words,
the script block determines which objects are permitted to continue down the pipeline and
which are not.
Cmdlet vs Function
Cmdlet - Written using .NET language and compiled using .NET libraries. They come pre-
compiled and therefore cannot be customized.
Function – a script written in powershell language using the cmdlets provided by powershell.
They can be customized- we can add scope, update parameters etc.
Example
function Get-CustomService {
$services = Get-Service
return $services
}
https://learn.microsoft.com/en-us/powershell/module/nettcpip/set-netipv4protocol?
view=windowsserver2022-ps