Get-Location
or pwd
Set-Location [path]
or cd [path]
cd ~
cd -
Example:
PS C:UsersUser> Get-Location
Path
----
C:UsersUser
PS C:UsersUser> Set-Location C:Windows
PS C:Windows>
Get-ChildItem
or dir
or ls
Get-ChildItem -Force
Get-ChildItem | Format-List
Example:
PS C:> Get-ChildItem -Force
Directory: C:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d--hs- 11/14/2023 10:14 PM $Recycle.Bin
d--hsl 7/12/2023 3:51 AM Documents and Settings
d----- 7/12/2023 1:06 PM PerfLogs
d-r--- 7/12/2023 3:51 AM Program Files
d-r--- 7/12/2023 3:52 AM Program Files (x86)
d----- 7/12/2023 1:06 PM Users
d----- 11/15/2023 9:32 AM Windows
New-Item -ItemType File -Name [filename]
New-Item -ItemType Directory -Name [dirname]
New-Item -ItemType Directory -Path [path/to/dirname] -Force
Example:
PS C:> New-Item -ItemType File -Name newfile.txt
PS C:> New-Item -ItemType Directory -Name newdir
PS C:> New-Item -ItemType Directory -Path deeply
esteddirectory -Force
Copy-Item [source] [destination]
Copy-Item [source] [destination] -Recurse
Move-Item [source] [destination]
Remove-Item [filename]
Remove-Item [dirname] -Recurse -Force
Example:
PS C:> Copy-Item file1.txt file2.txt
PS C:> Move-Item file2.txt newname.txt
PS C:> Remove-Item newname.txt
PS C:> Remove-Item olddir -Recurse -Force
Get-Content [filename]
Get-Content [filename] -Head 10
Get-Content [filename] -Tail 10
Example:
PS C:> Get-Content log.txt
PS C:> Get-Content log.txt -Tail 5
Select-String -Path [filename] -Pattern [pattern]
Get-ChildItem -Recurse | Select-String -Pattern [pattern]
Example:
PS C:> Select-String -Path log.txt -Pattern "error"
PS C:> Get-ChildItem -Recurse | Select-String -Pattern "TODO"
Invoke-Item [filename]
Example:
PS C:> Invoke-Item config.txt
Get-Process
Get-Process -Name [processname]
Example:
PS C:> Get-Process
PS C:> Get-Process -Name chrome
Start-Process [path/to/executable]
Stop-Process -Name [processname]
Stop-Process -Id [processID]
Example:
PS C:> Start-Process notepad.exe
PS C:> Stop-Process -Name notepad
Get-NetIPConfiguration
Test-NetConnection [destination]
Get-DnsClientCache
Example:
PS C:> Get-NetIPConfiguration
PS C:> Test-NetConnection www.google.com
Invoke-WebRequest -Uri [URL] -OutFile [filename]
Test-Connection [destination]
Example:
PS C:> Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"
PS C:> Test-Connection 8.8.8.8
Get-ComputerInfo
Get-HotFix
Get-Disk
Example:
PS C:> Get-ComputerInfo | Select-Object WindowsProductName, OsVersion, OsArchitecture
command1 | command2
Example:
PS C:> Get-Process | Where-Object {$_.CPU -gt 10} | Sort-Object CPU -Descending
Get-Alias
New-Alias -Name [alias] -Value [command]
Example:
PS C:> New-Alias -Name proc -Value Get-Process
$variableName = value
$variableName.GetType()
Example:
PS C:> $name = "John"
PS C:> $age = 30
PS C:> $name.GetType()
$array = @(1, 2, 3, 4)
$hash = @{Key1 = "Value1"; Key2 = "Value2"}
Example:
PS C:> $fruits = @("Apple", "Banana", "Orange")
PS C:> $person = @{Name = "Alice"; Age = 25; City = "New York"}
if (condition) {
# code
} elseif (condition) {
# code
} else {
# code
}
foreach ($item in $collection) {
# code
}
for ($i = 0; $i -lt 10; $i++) {
# code
}
Example:
PS C:> foreach ($file in Get-ChildItem) {
>> Write-Host $file.Name
>> }
function FunctionName {
param (
[Parameter(Mandatory=$true)][string]$param1,
[int]$param2 = 0
)
# Function body
}
Example:
PS C:> function Greet {
>> param ([string]$name)
>> Write-Host "Hello, $name!"
>> }
PS C:> Greet -name "Alice"
Hello, Alice!
Import-Module [ModuleName]
Get-Command -Module [ModuleName]
Example:
PS C:> Import-Module ActiveDirectory
PS C:> Get-Command -Module ActiveDirectory
try {
# Code that might cause an error
} catch {
# Error handling code
} finally {
# Code that runs regardless of error
}
Example:
PS C:> try {
>> $result = 10 / 0
>> } catch {
>> Write-Host "An error occurred: $_"
>> }
An error occurred: Attempted to divide by zero.
Enable-PSRemoting
Enter-PSSession -ComputerName [hostname]
Invoke-Command -ComputerName [hostname] -ScriptBlock { command }
Example:
PS C:> Enter-PSSession -ComputerName Server01
[Server01]: PS C:>
Get-ExecutionPolicy
Set-ExecutionPolicy [PolicyName]
Get-AuthenticodeSignature [script.ps1]
Example:
PS C:> Get-ExecutionPolicy
Restricted
PS C:> Set-ExecutionPolicy RemoteSigned
2024 © All rights reserved - buraxta.com