This repository was created to remember me regarding the powershell and its commands.
Microsoft learning platform stated that 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. It is used in many IT-related job roles for tasks that require system management, automation, and scripting.
Cloud Engineers / DevOps Engineers, System Administrators, Network Administrators, Information Security Analysts, Database Administrators (DBAs), Software Engineers, and Data Engineers
- PowerShell in Windows: Go to the Start menu, type “PowerShell,” and select Windows PowerShell or Windows PowerShell ISE (the Integrated Scripting Environment).
- Run as Administrator: Right-click on PowerShell and choose "Run as Administrator" if elevated permissions are needed.
Here are some basic commands to help you get familiar with PowerShell.
| Command | Description |
|---|---|
Get-Help |
Displays help about commands and syntax. |
Get-Command |
Lists all available commands in PowerShell. |
Get-Process |
Displays currently running processes. |
Get-Service |
Lists all services on the computer. |
Get-Location |
Shows the current directory. |
Set-Location |
Changes the current directory (similar to cd). |
Clear-Host (or cls) |
Clears the console screen. |
Get-Content |
Reads the contents of a file. |
Example:
Get-Help Get-ProcessThis displays information on how to use the Get-Process command.
PowerShell allows you to navigate the file system, just like you would in a traditional command line:
- Change directory:
Set-LocationorcdSet-Location "C:\path\to\your\directory"
- List files and folders:
Get-ChildItemorlsGet-ChildItem
PowerShell provides simple commands to create, copy, move, and delete files and directories.
| Command | Description |
|---|---|
New-Item |
Creates a new file or folder. |
Copy-Item |
Copies a file or folder. |
Move-Item |
Moves a file or folder. |
Remove-Item |
Deletes a file or folder. |
Examples:
# Create a new text file
New-Item -Path "C:\path\to\your\file.txt" -ItemType File
# Copy a file
Copy-Item -Path "C:\path\to\your\file.txt" -Destination "C:\path\to\new\location\"
# Move a file
Move-Item -Path "C:\path\to\your\file.txt" -Destination "C:\path\to\new\location\"
# Delete a file
Remove-Item -Path "C:\path\to\your\file.txt"PowerShell is highly effective for viewing and filtering data, especially for system information and file content.
-
Filter with
Where-Object:Get-Process | Where-Object { $_.CPU -gt 100 }
This shows only processes using more than 100 units of CPU time.
-
Select specific properties with
Select-Object:Get-Process | Select-Object -Property Name, CPU
This displays only the
NameandCPUcolumns of each process.
You can store data in variables for later use in PowerShell by prefixing the variable name with $.
$myVariable = "Hello, PowerShell!"
$number = 42To print a variable’s value:
Write-Output $myVariablePowerShell makes it easy to create, write, and read text files.
- Write text to a file:
"Hello, PowerShell!" | Out-File -FilePath "C:\path\to\your\file.txt"
- Read text from a file:
Get-Content -Path "C:\path\to\your\file.txt"
PowerShell supports loops (for, foreach, while) and conditional statements (if, else) for automating tasks.
-
For loop:
for ($i = 1; $i -le 5; $i++) { Write-Output "Number: $i" }
-
If statement:
$num = 5 if ($num -gt 3) { Write-Output "$num is greater than 3" } else { Write-Output "$num is less than or equal to 3" }
-
List running processes:
Get-Process -
Stop a process:
Stop-Process -Name "notepad" -Force
-
List services:
Get-Service -
Start or stop a service:
Start-Service -Name "wuauserv" # Start Windows Update service Stop-Service -Name "wuauserv" # Stop Windows Update service
PowerShell’s scripting capabilities let you automate complex tasks by writing scripts (.ps1 files) that run a series of commands.
-
Create a Script:
- Open a text editor (such as Notepad) and write your commands.
- Save the file with a
.ps1extension, such asMyScript.ps1.
-
Run a Script:
.\MyScript.ps1
Ensure that PowerShell’s execution policy allows scripts to run:
Set-ExecutionPolicy RemoteSigned
- to get all lines in txt file
Get-Content -Path "C:\path\to\your\file.txt" -Wait- to append text to a file
Add-Content -Path "C:\path\to\your\file.txt" -Value "Your text to append"- to replace text in a file
(Get-Content -Path "C:\path\to\your\file.txt") -replace 'old text', 'new text' | Set-Content -Path "C:\path\to\your\file.txt"- to edit specific line
# Read the file into an array, where each line is an element
$file = Get-Content -Path "C:\path\to\your\file.txt"
# Modify a specific line (e.g., line 2)
$file[1] = "This is the new content for line 2"
# Write the modified content back to the file
$file | Set-Content -Path "C:\path\to\your\file.txt"- to insert Text at a Specific Line
$file = Get-Content -Path "C:\path\to\your\file.txt"
# Insert text at a specific line (e.g., insert after line 2)
$newContent = @("Inserted line")
$file = $file[0..1] + $newContent + $file[2..($file.Count - 1)]
# Write the modified content back to the file
$file | Set-Content -Path "C:\path\to\your\file.txt"- to remove Lines from a File
$file = Get-Content -Path "C:\path\to\your\file.txt"
# Remove line 2 (e.g., by omitting index 1)
$file = $file | Where-Object { $_ -ne $file[1] }
# Write the updated content back to the file
$file | Set-Content -Path "C:\path\to\your\file.txt"- to monitor a File for Real-Time Updates
Get-Content -Path "C:\path\to\your\file.txt" -Tail 10 -Wait- Use Aliases: Many common PowerShell commands have aliases (
lsforGet-ChildItem,cdforSet-Location,catforGet-Content). Use them for quicker access. - Piping (
|): Use the pipeline to chain commands together for efficient processing. - Error Handling: Use
TryandCatchblocks to manage errors in scripts.
To copy files from one folder to another, then create a backup:
$source = "C:\path\to\source\folder"
$destination = "C:\path\to\backup\folder"
# Create the backup folder if it doesn't exist
if (!(Test-Path -Path $destination)) {
New-Item -ItemType Directory -Path $destination
}
# Copy files
Copy-Item -Path "$source\*" -Destination $destination -Recurse
Write-Output "Backup completed successfully!"- Explore cmdlets: Use
Get-CommandandGet-Helpto discover and learn new cmdlets. - Microsoft PowerShell Documentation: Microsoft offers comprehensive documentation, tutorials, and example scripts.