Skip to content

Instantly share code, notes, and snippets.

@chirag64
Last active May 11, 2023 22:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chirag64/56008a886f3fa4ce7a93 to your computer and use it in GitHub Desktop.
Save chirag64/56008a886f3fa4ce7a93 to your computer and use it in GitHub Desktop.
PowerShell snippets
#Create array of file list from a directory.
$arr = (dir).FullName #Get all files
$arr = (dir *.log).FullName #Get files of .log extension
#Get headers of file to identify its format.
Get-Content -Path FilePath -Encoding Byte -TotalCount 4
#Download File via http.
Invoke-WebRequest "http://filepath/file.jpg" -OutFile "C:\LocalPath\filename.jpg"
#Rename files in Bulk.
dir | Rename-Item -NewName {$_.name -replace ".jpg",".png"} #Renames all jpgs to pngs
#Count instances of string in file.
Get-Content .\file1.txt | Where-Object ({$_ -match str}) | Measure-Object -Line
#Regex grep -oP equivalent
Select-String -Path .\myfile.csv -Pattern '^Ava.+' -AllMatches | % { $_.Matches } | % { $_.Value }
#From <http://www.gfi.com/blog/windows-powershell-extracting-strings-using-regular-expressions/>
#Run C# code in PowerShell
[System.Windows.Forms.MessageBox]::Show('Hello world')
#Run complex C# code in PowerShell:
$source = @"
public class BasicTest
{
public static int Add(int a, int b)
{
return (a + b);
}
public int Multiply(int a, int b)
{
return (a * b);
}
}
"@
Add-Type -TypeDefinition $source
[BasicTest]::Add(4, 3)
$basicTestObject = New-Object BasicTest
$basicTestObject.Multiply(5, 2)
#From <http://technet.microsoft.com/en-us/library/hh849914.aspx>
#Simple UI Example:
#Simple task manager to stop process:
Get-Process |
Out-GridView -PassThru |
Stop-Process
#From <http://blogs.technet.com/b/heyscriptingguy/archive/2014/08/21/powershell-mini-scripting-games-2014-answer-4.aspx>
#UI of a PowerShell cmdlet:
Show-Command -Name Get-Process
#PS1 equivalent:
function prompt
{
Write-Host "$env:USERNAME @ $env:COMPUTERNAME - $(Get-Location) [$(Get-Date -Format "HH:mm:ss")] > " -ForegroundColor Cyan -BackgroundColor DarkBlue -NoNewline;
return " "
}
#Create new column based on existing column
#List all commands that have length >= 41 and calculate & show their length in separate column:
Get-Command | select name, @{LABEL='length';EXPRESSION={($_.name.tostring().length)}} | sort length -Descending | where length -ge 41
#From <http://blogs.technet.com/b/heyscriptingguy/archive/2014/09/07/weekend-scripter-don-t-break-powershell-script-with-line-breaks.aspx>
#Bulk convert all images to jpg
function ConvertTo-Jpg {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path
)
process {
if ($Path -is [string]) {
$Path = get-childitem $Path
}
$Path | foreach {
$image = [System.Drawing.Image]::FromFile($($_.FullName))
$FilePath = "{0}\{1}.jpg" -f $($_.DirectoryName), $($_.BaseName)
$image.Save($FilePath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment