Powershell Basic
Why Powershell?
Powershell is built on top of .NET framework, making it convenient access to:
the .NET Framework API
Component Object Model (COM)
Windows Management Instrumentation (WMI)
Powershell Executable
For 64-bit system, there are 2 locations of powershell:
For 32-bit system, the location of powershell:
To check the environment:
Fucking Basic
Run as Administrator
Call Help
Use a Different Version
Launch without Profiles
Execution Policy
Remember this is not for defensive purpose! No admin right is needed for this operation.
Run in background
Powershell Commands from CMD
Base64 encoded Commands
Man in PS
List Options Available
Output Format
By default, the output will be in column format. But you can output differently:
Also you can sort the output:
Sort and select a field
Suppress Error Message
Cmdlets (Command-Lets)
Cmdlets are:
Lightweight Powershell scripts that perform a single function
Instances of .NET Framework classes derived from the Cmdlet Base Class and provide access to system functions
Native commands in Powershell
Written in a "Verb-Noun" filename format which tells their function (e.g. Invoke-Command)
Use pipeline | to return output to other Cmdlets
Get-Process
To get all of the Properties:
For example:
Get-ChildItem
By default there are 3 alias:
Get-WmiObject
Return information about WMI objects
For example, if we want to get verbose OS information:
Get Services:
Export-CSV
Output result ... For example
Accessing Registry
Convenient method in Powershell ...
Get-Content (cat)
Select-String: Grep
For example:
Also, we can do a for-loop to get all content from txt files in a folder:
Note %
stands for ForEach-Object
and $_
stands for current values in the pipeline
This is like grep
in linux command line:
Get-Service
List all installed Services:
Search for a specific service
Modules
Modules typically have .psm1
file extension.
Types of modules:
Script Modules (Most common)
Binary Modules
Manifest Modules
Dynamic Modules
Get-Modules
To see imported modules:
To see all modules available:
Import-Module
For example, if you have downloaded a Module from a GitHub project. To use it, you have to first import the module:
After importing, you can view the available commands:
Scripts
Usually ends with .ps1
Example script - cat.ps1
When run:
Note it asks for input file
since we specify mandatory=$true
Looping
In Powershell, we can do looping using:
for()
foreach()
while()
do {xxxxxx} while()
do {xxxxxx} until()
Loop Statement (xxx)
and Loop Body {xxxxx}
(xxx)
and Loop Body {xxxxx}
Another way to do looping, which we typically do | ForEach-Object { xxx $_.property xxx}
Where-Object - Filtering Result
Mini Port Scanner
Objects
To be simple, an object is:
a collections of properties
with methods
Get-Member
To see the methods available for an Object:
Property:
For example, if we want to kill a process:
.NET Objects
WebClient
For example, if we're attacking a remote host and you want it to import a script ... Like Import a Mimikatz in memory:
Last updated