Skip to content

Instantly share code, notes, and snippets.

View PrateekKumarSingh's full-sized avatar

Prateek Singh PrateekKumarSingh

View GitHub Profile
$Timer = [System.Diagnostics.Stopwatch]::StartNew()
$maxThreads = 20
$RunSpacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, $maxThreads )
$Jobs = New-Object System.Collections.ArrayList
$RunSpacePool.Open()
$Worker = {
param($Item)
AudioDeviceCmdlets
AutomatedLab
AutomatedLab.Common
AutomatedLabDefinition
AutomatedLabNotifications
AutomatedLabUnattended
AutomatedLabWorker
Az.Accounts
Az.Aks
Az.AnalysisServices
# method 1
$a = 1..5
[array]::Reverse($a)
$a
# method 2
$a = 1..5
$a[$a.count..0]
# Method 1 - Using Expand-Archive cmdlet
Expand-Archive -Path C:\Data\Compressed.zip -DestinationPath C:\Data\one -Verbose
# Method 2 - Using .Net ZipFile Class
Add-Type -Assembly "System.IO.Compression.Filesystem"
[System.IO.Compression.ZipFile]::ExtractToDirectory('C:\Data\Compressed.zip','C:\Data\two')
# Method 3 - Using Shell.Application COM object
$ZippedFilePath = "C:\Data\Compressed.zip"
$DestinationFolder = "C:\Data\three\"
Function Malicious {
#Get current user context
$CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
#Check user is running the script is member of Administrator Group
if($CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{
Write-host "Script is running with Administrator privileges!"
}
else
# Method 1 - Using PSTypeNames
$Alpha = [PSCustomObject]@{a=1; b=2}
$Alpha.pstypenames.Clear()
$Alpha.pstypenames.Add('Alpha')
$Alpha | Get-Member
# Method 2 - Using PSObject Adapter
$Beta = Get-Service BITS
$Beta.PSObject.TypeNames.Insert(0,'Beta')
$Beta | Get-Member
$URLs = "https://aka.ms/wsl-ubuntu-1804" ,"https://aka.ms/wsl-ubuntu-1804-arm" ,"https://aka.ms/wsl-ubuntu-1604" ,"https://aka.ms/wsl-debian-gnulinux" ,"https://aka.ms/wsl-kali-linux" ,"https://aka.ms/wsl-opensuse-42" ,"https://aka.ms/wsl-sles-12"
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
Foreach($URL in $URLs){
$Filename = "$(Split-Path $URL -Leaf).appx"
Write-Host "Downloading: $Filename" -Foreground Yellow -NoNewline
try{
Invoke-WebRequest -Uri $URL -OutFile $Filename -UseBasicParsing
Add-AppxPackage -Path $Filename
$URL = 'https://aka.ms/wsl-ubuntu-1804'
$Filename = "$(Split-Path $URL -Leaf).appx"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $URL -OutFile $Filename -UseBasicParsing
Invoke-Item $FileName
$Error.count # empty automatic variable
# throwing an error when file doesn't exists
Get-Item C:\Data\nosuchfile.txt
$Error.count # one error record logged
# supressing the error
Get-Item C:\Data\nosuchfile.txt -ErrorAction SilentlyContinue
$Error.count # but still capturing the error
Function Get-FolderSize {
param(
$Path = (Get-Location),
$Top = 10,
[Switch] $ShowErrors
)
$Start = Get-Date
$FSO = New-Object -ComObject Scripting.FileSystemObject -ErrorAction Stop
$Folders = Get-ChildItem $Path -Recurse -ea Silentlycontinue -ErrorVariable +err | Where-Object { $_.PSIscontainer } | Select-Object -ExpandProperty fullname