Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PowerShell

PowerShell for Pentesters

Table of Contents

  1. Basics
    1. PowerShell-Basic
      1. cd
      2. dir
      3. ls
      4. ps
      5. Get-Help
      6. Update-Help
      7. Wildcard
    2. Exercises
  2. Cmdlets
    1. Exploring and Using Cmdlets
      1. Get-Command
      2. Get-Process
      3. Get-Service
      4. Start-Process
      5. Stop-Process
      6. Get-HotFix
      7. Get-Help_
    2. Exercises2
      1. Get-ComputerInfo
      2. Get-Content
      3. Get-History
      4. Get-PSDrive
      5. Get-LocalGroup
      6. Clear-History
      7. Invoke-Command
      8. Import-Module
      9. Get-DnsClient
      10. Get-NetRoute
  3. Output Formatting
    1. Formatting
      1. Format-Table
      2. Format-List
    2. Output Manipulation
      1. Out-GridView
      2. Out-File
  4. Operators
    1. Arithmetic
    2. Assignment
    3. Comparison
    4. Redirection
    5. Exercise3
      1. Static member operator
      2. split
  5. Advanced Operators
    1. Logical
      1. -and
      2. -or
      3. -xor
      4. -not
      5. Negates
    2. Split and Join
      1. -split
      2. -join
    3. Type-Operations
      1. -is
      2. -isnot
      3. -as
  6. Types
    1. Get-Type
  7. Arrays
    1. Type Conversion
    2. Array
  8. Conditional Statements
    1. if elseif else
    2. Switch Statement
    3. Switch -Wildcard
    4. Switch -Regex
  9. Loop Statements
    1. while
    2. foreach
    3. Loop Cmdlets
      1. ForEach-Object
      2. Where-Object
    4. Exercise4
  10. Basic Scripting
    1. PowerShell ISE
    2. Get-ExecutionPolicy
    3. Set-ExecutionPolicy
  11. Functions Part 1
    1. Simple Usage
    2. Parameters of a PowerShell function
      1. $args
      2. Declaring parameters
      3. Positional and named parameters
  12. Functions Part 2
    1. Dynamic number of Parameters
    2. Type declaration of the Parameters
    3. Default Values
  13. Functions Part 3
    1. Switch Parameters
    2. Returning values
    3. Scope of variables and functions
    4. Exercises5
  14. Advanced Functions
    1. Param Statement
    2. Parameter Attributes
      1. Mandatory

Basics

PowerShell-Basic

cd

cd \

cd

dir

dir

dir

ls

ls

ls

ps

ps

ps

Get-Help

Get-Help |more

`Get-Help |more

Update-Help

Install Update-Help your computer. Enter Y

Get-Help Get-Help -Examples |more

Get-Help

Wildcard

Get-Help * |more

Get-Help * |more

Get-Help *process

Get-Help *process

Get-Process

Get-Process

Get-Help *alias*

Get-Help alias

Get-Alias

Get-Alias

Get-Help Get-Help -Examples |more

Get-Help Get-Help -Examples |more

Get-Help about_Aliases |more

Get-Help about_Aliases |more

Exercises

Use Get-Help to retrieve help about Get-Command:

Get-Help Get-Command |more

Get-Command

Use Get-Help about_[topic] to retrieve help about powershell.exe:

Get-Help powershell

Get-Help powershell

Get-Help about_PowerShell.exe

Get-Help about_PowerShell.exe

Cmdlets

Exploring and Using Cmdlets

Get-Command

DESCRIPTION

The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions,workflows, filters, scripts, and applications. Get-Command gets the commands from Windows PowerShell modules and snap-ins and commands that were imported from other sessions. To get only commands that have been imported into the current session, use the ListImported parameter.

Get-Help Get-Command |more

Get-Help Get-Command |more

Get-Command |more

Get-Command |more

Get-Command -CommandType Cmdlet |more

Get-Command -CommandType Cmdlet |more

REMARKS

To see the examples, type: "get-help Get-Command -examples". For more information, type: "get-help Get-Command -detailed". For technical information, type: "get-help Get-Command -full". For online help, type: "get-help Get-Command -online"

Get-Help Get-Command -full |more

Get-Help Get-Command -full |more

PARAMETERS

Get-Help Get-Command -Parameter * |more

Get-Help Get-Command -Parameter * |more

Cmdlet Process

Get-Command -CommandType Cmdlet -Name *process*

Get-Command -CommandType Cmdlet -Name process

Cmdlet Service

Get-Command -CommandType Cmdlet -Name *service*

Get-Command -CommandType Cmdlet -Name service

Measure-Object

810 Cmdlet Installed

Get-Command -CommandType Cmdlet |Measure-Object

Get-Command -CommandType Cmdlet |Measure-Object

Get-Process

Get-Process |more

Get-Process |more

Get-Service

Get-Service |more

Get-Service |more

Get-Command -Verb stop

Get-Command -Verb stop

Get-Command -Verb start

Get-Command -Verb start

Start-Process

Get-Help Start-Process -Examples |more

Get-Help Start-Process -Examples |more

Start-Process -FilePath notepad.exe

Start-Process -FilePath notepad.exe

Stop-Process

Stop-Process -Name notepad

Get-Process notepad

Get-Process notepad

Stop-Process -Id 6292

Stop-Process -Id 6292

Get-HotFix

Get-HotFix

Get-HotFix

Get-Help_

Get-Help *cmdlets*

Get-Help cmdlets

Get-Help *command*

Get-Help command

Get-Help about_Core_Commands |more

Get-Help about_Core_Commands |more

Exercises2

Explore cmdlets using Get-Command and pick ten cmdlets which could be useful in penetration tests.

Get-ComputerInfo

Get-ComputerInfo

Get-ComputerInfo

Get-Content

Get-Content .\log.txt.txt -TotalCount 5 |Set-Content output.txt

Get-Content

Get-History

Get-History |more

Get-History

Get-PSDrive

Get-PSDrive

Get-PSDrive

Get-LocalGroup

Get-LocalGroup

Get-LocalGroup

Clear-History

Clear-History

Clear-History

Invoke-Command

Get-Help Invoke-Command -Examples |more

Invoke-Command

Import-Module

Get-Help Import-Module -Examples |more

Import-Module

Get-DnsClient

Get-Command -Module DnsClient * |more

Get-DnsClient

Get-NetRoute

Get-Command -Module NetTCPIP |more

Get-NetRoute

Output Formatting

Formatting

Get-Command -CommandType Cmdlet -Name format*

Output Formatting

Format-Table

Get-ChildItem |Format-Table

Format-Table

Get-ChildItem |Format-Table Name

Get-ChildItem |Format-Table Name

Format-List

Format-List

Output Manipulation

Get-Command -CommandType Cmdlet -Name out*

Get-Command -CommandType Cmdlet -Name out*

Out-GridView

Get-Process |Out-GridView

Get-Process |Out-GridView

Out-File

Get-Process |Out-File -FilePath get_process.txt

Get-Process |Out-File -FilePath get_process.txt

Get-Content .\get_process.txt |more

Get-Content .\get_process.txt |more

Get-ChildItem |Format-List * |Out-File -FilePath 'C:\Users\anake\Downloads\list.txt'

Get-ChildItem |Format-List * |Out-File

Operators

Arithmetic

+ , - , * , / , %

Arithmetic

Assignment

= , += , -= , *= , /= , %=

Assignment

Comparison

-eq , -ne , -gt , -lt , -le , -ge , -match , -notmatch , -replace , -like , -notlike , -in , -notin , -contains , -notcontains

Comparison

Redirection

> , >> , 2> , 2>&1

>

Redirection

>>

Redirection2

2>&1

Redirection3

Exercise3

Explore the PowerShell help system and locate help topics for various operators.

Get-Help Arith |more

Get-Help Arith |more

SEE ALSO

about_Arithmetic_Operators

about_Assignment_Operators

about_Comparison_Operators

about_Logical_Operators

about_Type_Operators

about_Split

about_Join

about_Redirection

Static member operator

Calls the static properties operator and methods of a .NET Framework class. To find the static properties and methods of an object, use the Static parameter of the Get-Member cmdlet.

[datetime]::now

datetime

split

$i = 1

$c -split {if ($i -lt 1) {$_ -eq ","} else {$_ -eq ";"}}

split

Advanced Operators

Logical

-and

Get-Help about_Comparison -Examples |more

Get-Help about_Logical -Examples |more

-and

-or

-or

-xor

-xor

-not

-not

Negates

Negates

Split and Join

-split

-split

-join

-join

Type-Operations

-is

-is

-isnot

-isnot

-as

-as

Types

Get-Type

PSObject and the Adapted and Extended Type Systems (ATS and ETS) https://blogs.msdn.microsoft.com/besidethepoint/2011/11/22/psobject-and-the-adapted-and-extended-type-systems-ats-and-ets/

Get-Type

strings

strings1

strings2

Arrays

Type Conversion

Type Conversion

conv

Array

Commands in PowerShell return an array of Objects - Object[]

array

Index

array2

More than one type of elements could be stored.

empty

Conditional Statements

if elseif else

if elseif else

Switch Statement

Switch Statement

Switch -Wildcard

switch -Wildcard

Switch -Regex

Get-WindowsUpdateLog

Get-WindowsUpdateLog

Switch -Regex -File C:\Users\anake\OneDrive\Desktop\WindowsUpdate.log { 'Validating'{$_}}

Switch -Regex -File

Loop Statements

while

while

foreach

foreach

Loop Cmdlets

ForEach-Object

ForEach-Object

Where-Object

Get-ChildItem |Where-Object {$_.Name -match "txt"}

Where-Object

Exercise4

Iterate through the process running on your computer and print the path of the executable for each process.

Get-Process |ForEach-Object -MemberName Path

Get-Process |ForEach-Object -MemberName Path

Basic Scripting

PowerShell ISE

PowerShell ISE

Get-ExecutionPolicy

Get-ExecutionPolicy

exec

Set-ExecutionPolicy

Run PowerShell as Administrator

Set-ExecutionPolicy Bypass

Set-ExecutionPolicy

Set-Execu

Set-ExecutionPolicy Restricted

Executio

Functions Part 1

Simple Usage

Simple Usage

Parameters of a PowerShell function

$args

Simple Usage2

Declaring parameters

Simp

declare

Positional and named parameters

Positional and named parameters

Functions Part 2

Dynamic number of Parameters

Dynamic number of Parameters

Dynamic number of Par

Type declaration of the Parameters

Type declaration

addstring

Default Values

Default Values

Functions Part 3

Switch Parameters

Switch Parameters

Returning values

Returning values

Scope of variables and functions

Scope of variables and functions

Exercises5

Create a function which accepts name of a process or service and stop it.

PS C:\Users\anake> function stopped ($serv){
>> if ($serv) {Stop-Process -Name $serv}
>> }
PS C:\Users\anake> stopped MicrosoftEdge

stopped

Use a switch variable in the above function to add the ability of stopping a service as well

PS C:\Users\anake> function stopservice ($service, [switch]$stop){
>> $service
>> if ($stop) {Stop-Process -Name $service}
>> }
PS C:\Users\anake> stopservice MicrosoftEdge -stop
MicrosoftEdge

stopservice

Accept a PID parameter too. If a PID is passed to the function, attempt should be made only to stop a process.

PS C:\> function StoopService ($service, [switch]$stop, [switch]$id){
>> $service
>> if ($stop) {Stop-Process -Name $service}
>> if ($id) {Stop-Process -Id $service}
>> }

idd

Advanced Functions

Param Statement

Parameter Attributes

Mandatory

mandatory

About

PowerShell for Pentesters

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors