Skip to main content

Batch and Powershell

Active Directory

Load the built-in Active Directory PowerShell module

Use one of the following commands to install the Active Directory module (source):

Add-WindowsFeature RSAT-AD-PowerShell
Enable-WindowsOptionalFeature -FeatureName ActiveDirectory-Powershell -Online -All

Then, load the module:

PowerShell
Import-Module ActiveDirectory

List domain users using Get-ADUser and filter by the username

PowerShell
Get-ADUser -Filter * | select Name

Check the description field of domain users for sensitive data such as passwords

PowerShell
Get-DomainUser * | Select-Object samaccountname,description

Applications, processes, and services

Show running processes

cmd.exe
tasklist
tasklist /svc
PowerShell
Get-Process
Get-Process <process> # the name must be exact

External resources:

Users and groups

Current user security privileges

cmd.exe
whoami /priv

Current user local groups

cmd.exe
net localgroup

File System

Absolutely, here are some common PowerShell and Batch commands.

Display List of Files and Directories

Using dir
dir
Using Get-ChildItem
Get-ChildItem

Changing Directory

Using cd
cd \path\to\directory
Using Set-Location
Set-Location -Path \path\to\directory

Creating a New Directory

Using mkdir
mkdir new_directory
Using New-Item
New-Item -ItemType Directory -Path .\new_directory

Deleting a File

Using del
del filename
Using Remove-Item
Remove-Item -Path filename

Copying Files

Using copy
copy source destination
Using Copy-Item
Copy-Item -Path source -Destination destination

Displaying Content of a File

Using type
type filename
Using Get-Content
Get-Content -Path filename

Find files by content

Search for 'password' using findstr
findstr /sim /c:"password" *.txt *.ini *.cfg *.config *.xml *.git *.ps1 *.yml
Command explanation
  • /s searches for matching files in the current directory and all subdirectories
  • /i makes the search case-insensitive
  • /m only prints the file names of matching files
  • /c specifies the string to search for

External resources:

Mount SMB share

Using net use
net use Z: \\server\share /user:username password
Using New-PSDrive
New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\share -Credential username

Move file to unmounted SMB share

Using copy
copy file.txt \\server\share
Using Copy-Item
Copy-Item -Path file.txt -Destination \\server\share