Last active
July 27, 2017 17:11
-
-
Save chirag64/6d8d3fcdafe6cbb5d67c to your computer and use it in GitHub Desktop.
PowerShell profile - .bashrc equivalent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
%windir%\system32\WindowsPowerShell\v1.0\profile.ps | |
This is for all users of the computer and for all shells. | |
%windir%\system32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 | |
This is for all users of the computer, but it is only for the Microsoft.PowerShell shell. | |
%UserProfile%\Documents\WindowsPowerShell\profile.ps1 | |
This is for the current user only and all shells. | |
%UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 | |
This is for the current user only and only for the Microsoft.PowerShell shell. | |
#> | |
#This file is only loaded for PowerShell ISE | |
Import-Module Microsoft.Online.SharePoint.PowerShell -ErrorAction Ignore | |
function prompt | |
{ | |
if ($? -eq $true) | |
{ | |
#Write-Host -ForegroundColor Green "OK" | |
Write-Host -ForegroundColor Green ":)" | |
} | |
else | |
{ | |
#Write-Host -ForegroundColor Red "Error" | |
Write-Host -ForegroundColor Red ":(" | |
} | |
Write-Host "$env:USERNAME @ $env:COMPUTERNAME - $(Get-Location) [$(Get-Date -Format "HH:mm:ss")] > " -ForegroundColor Cyan -BackgroundColor DarkBlue -NoNewline; | |
return " " | |
} | |
function When-File-Doesnt-Exist { | |
<# | |
.SYNOPSIS | |
Runs as long as a specified file exists. | |
.DESCRIPTION | |
Used for pointing to a temporary file that is currently downloading. Runs as long as a specified file exists. Useful for executing a program after the particular file is finished downloading. | |
.PARAMETER Path | |
Path to temporary file that is being downloaded | |
.PARAMETER Seconds | |
Interval in seconds to check if the file still exists. Default is 5 seconds. | |
.PARAMETER Silent | |
Switch that specifies if the function should be verbose or silent | |
.EXAMPLE | |
Try to make the computer make a long beep after a download is complete. | |
PS C:\> When-File-Doesnt-Exist myfile.zip.crdownload; [console]::Beep(2000,2000) | |
File exists at: 18:13:24 | |
File exists at: 18:13:29 | |
.EXAMPLE | |
Try to shutdown the computer after a download is complete with a 2 second interval and no verbose output. | |
PS C:\> When-File-Doesnt-Exist -File "myfile.zip.crdownload" -Seconds 2 -Silent; shutdown /s /t 0 | |
.NOTES | |
Author: Chirag Bhatia (@chirag64) | |
Repository: https://github.com/chirag64 | |
#> | |
param ( | |
[Parameter(Mandatory=$true)][string] $Path, | |
[float] $Seconds = 5, | |
[switch] $Silent = $false | |
) | |
while (Test-Path -Path $Path) { | |
if (!$Silent) { | |
Write-Host "File exists at:" (Get-Date -Format "HH:mm:ss") | |
} | |
Start-Sleep -Seconds $Seconds | |
} | |
} | |
function say([string]$text) | |
{ | |
Add-Type -AssemblyName System.speech | |
$tts = New-Object System.Speech.Synthesis.SpeechSynthesizer | |
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer | |
$tts.Rate = -5 | |
$speak.Speak($text) | |
} | |
function Beep-When-Nets-Back | |
{ | |
$flag = $true | |
while ($flag) | |
{ | |
if (!((ping google.com -n 1 -i 1).ToString().Contains("try again"))) | |
{ | |
$flag=$false | |
} | |
sleep -Seconds 5; | |
} | |
[System.Console]::Beep(1500, 1000) | |
say "We are now online" | |
} | |
function Beep-When-Nets-Gone | |
{ | |
$flag = $true | |
while ($flag) | |
{ | |
if ((ping google.com -n 1 -i 1).ToString().Contains("try again")) | |
{ | |
$flag=$false | |
} | |
sleep -Seconds 5; | |
} | |
[System.Console]::Beep(1500, 1000) | |
say "We are now offline, internet connection is gone" | |
} | |
function AfterOffline([string]$ip) | |
{ | |
while (!(((ping -n 1 -i 1 $ip) -join '') -match "(timed\sout|unreachable)")) | |
{ | |
sleep 5 | |
} | |
} | |
function AfterOnline([string]$ip) | |
{ | |
while (((ping -n 1 -i 1 $ip) -join '') -match "(timed\sout|unreachable)") | |
{ | |
sleep 5 | |
} | |
} | |
function msg | |
{ | |
param | |
( | |
[string]$msgString | |
) | |
Try | |
{ | |
[System.Windows.Forms.MessageBox]::Show($msgString) | |
} | |
Catch [System.Exception] | |
{ | |
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") > $null | |
[System.Windows.Forms.MessageBox]::Show($msgString) | |
} | |
} | |
function beep($pitch = 500, $duration = 1000) | |
{ | |
[Console]::beep($pitch, $duration) | |
} | |
function vsdiff | |
{ | |
param | |
( | |
[string]$file1, | |
[string]$file2 | |
) | |
if (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe' -PathType Leaf) | |
{ | |
& 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe' /diff $file1 $file2 | |
} | |
else | |
{ | |
Write-Error "Visual Studio not found at: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" | |
return | |
} | |
} | |
function AfterRunning { | |
param ( | |
[String] $Process | |
) | |
while ((Get-Process $Process -ErrorAction SilentlyContinue | Select-Object -ExpandProperty ProcessName) -ne $null) { | |
Start-Sleep -Seconds 2 | |
Write-Host "Process still running..." | |
} | |
} | |
#Do nothing | |
function DoNothing { | |
return | |
} | |
function dw() { | |
cd ($HOME + '\Downloads') | |
} | |
function watch { | |
<# | |
.EXAMPLE | |
watch -path "C:\Users\chirag.\Downloads\folder" -changeType Changed -functionToCall $function:DoNothing | |
#> | |
param ([string]$path = (Get-Location), $changeType, [ScriptBlock]$functionToCall = $function:DoNothing) | |
# Watch a file changes in the current directory, | |
# Execute all tests when a file is changed or renamed | |
$watcher = New-Object System.IO.FileSystemWatcher | |
$watcher.Path = $path | |
$watcher.IncludeSubdirectories = $true | |
$watcher.EnableRaisingEvents = $false | |
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite -bor [System.IO.NotifyFilters]::FileName | |
while($true) | |
{ | |
$result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::All, 1000); | |
if($result.TimedOut){ | |
continue; | |
} | |
Write-Host $changeType | |
if ($changeType -eq $null -or $result.ChangeType -eq $changeType) | |
{ | |
Write-Host $(Get-Date -Form "HH:mm:ss") "-" $result.ChangeType "file" $result.Name | |
$functionToCall.Invoke() | |
} | |
} | |
} | |
#Function call | |
#watch -path "C:\Users\chirag.\Downloads\folder" -changeType Changed -functionToCall $function:DoNothing | |
function Get-Bytes { | |
<# | |
.SYNOPSIS | |
Outputs the contents of the file in bytes | |
.DESCRIPTION | |
Outputs the contents of the file in bytes | |
.EXAMPLE | |
Get-Bytes -File C:\myfile.exe -Bytes 4 | |
.EXAMPLE | |
Get-Bytes C:\myfile.exe 10 | |
.PARAMETER File | |
The file to output. Just one. | |
.PARAMETER Bytes | |
The number of bytes to output. Defaults to 4 | |
#> | |
param | |
( | |
[Parameter(Mandatory=$True, | |
ValueFromPipeline=$True, | |
ValueFromPipelineByPropertyName=$True, | |
HelpMessage='Which file would you like to use?')] | |
[string]$File, | |
[int]$Bytes = 4 | |
) | |
if (Test-Path $File -PathType Leaf) { | |
Write-Host -NoNewline (Get-Content -Encoding Byte -TotalCount $Bytes $File) | |
} | |
else { | |
Write-Error "Invalid file: '$File' does not seem to be a valid file." | |
} | |
} | |
# Chocolatey aliases if chocolatey is installed | |
if (Test-Path -Path "C:\ProgramData\chocolatey\bin\choco.exe") | |
{ | |
function i { choco install $args } | |
function ss { choco search $args } | |
function u { choco uninstall $args } | |
function uu { if ([bool]$args) { choco upgrade $args } else { choco upgrade all } } | |
} | |
# Write random quotes from files, current one is a json file from www.mentor.so | |
if (Test-Path -Path ".\Documents\WindowsPowerShell\quotes.json") | |
{ | |
$quote=(Get-Content ".\Documents\WindowsPowerShell\quotes.json" | Out-String | ConvertFrom-Json | Get-Random) | |
Write-Host ("`n" + $quote.content + "`n-" + $quote.user + "`n") -ForegroundColor Yellow | |
} | |
$CONFIG = "$HOME\Documents\WindowsPowerShell\profile.ps1" | |
function beeploop() | |
{ | |
while ($true) | |
{ | |
beep -duration 150 | |
sleep 60 | |
} | |
} | |
Set-Alias -Name yd -Value youtube-dl | |
function ym($url) { | |
#cd "E:\My Documents\My Music\YL"; | |
youtube-dl --prefer-ffmpeg --extract-audio --audio-format mp3 $url | |
} | |
function standby() { | |
cmd /c "C:\Windows\System32\rundll32.exe powrprof.dll,SetSuspendState Standby"; | |
} | |
function AtTime($SchTime) { | |
if ($SchTime -match "\d{4}") { | |
Write-Error "Incorrect format, use ':' for timings"; | |
return; | |
} | |
try { | |
$SchTime = Get-Date -Date $SchTime; | |
} | |
catch { | |
Write-Error "$SchTime is invalid time" | |
return; | |
} | |
if (((Get-Date) - $SchTime).TotalSeconds -gt 0) { | |
$TargetTime = $SchTime.AddDays(1); | |
} | |
else { | |
$TargetTime = $SchTime | |
} | |
Write-Host "Sleeping for" ([Math]::Floor(($TargetTime - (Get-Date)).TotalSeconds)) "seconds"; | |
Start-Sleep -Seconds ([Math]::Floor(($TargetTime - (Get-Date)).TotalSeconds)); | |
} | |
function sayTime() { | |
$nowTime = Get-Date; | |
if (($nowTime).Minute -eq 0) { | |
say ("It is now " + ($nowTime.ToString("hh") -as [int]) + " o clock"); | |
} | |
else { | |
say ("The time is now " + ($nowTime.ToString("hh") -as [int]) + " " + $nowTime.Minute); | |
} | |
} | |
function Pad-Bytes($path=".\") { | |
if (Test-Path $path -PathType "Container") { | |
$files = Get-ChildItem $path -File | |
} | |
elseif (Test-Path $path -PathType "Leaf") { | |
$files = @(Get-ChildItem $path) | |
} | |
else { | |
Write-Error "No file found at path, please check path again and retry" | |
} | |
ForEach ($f in $files) { | |
$LastBytes = Get-Content $f -Tail 4 -Encoding Byte | |
$NullBytes = @(13,0,10,0) | |
if ((Compare-Object $LastBytes $NullBytes).Length -ne 0) { | |
echo (" " * (Get-Random -Minimum 10 -Maximum 20)) >> $f | |
Write-Host "Written space characters to $f" -foregroundcolor green | |
} | |
else { | |
Write-Host "Skipping file $f" -foregroundcolor red | |
} | |
} | |
} | |
function List-UsbDevices(){ | |
return gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)}; | |
} | |
function Are-Headphones-Plugged() { | |
return [bool]((List-UsbDevices | Where-Object {$_.Description -match "Audio Device"} | Measure-Object).Count) | |
} | |
function Get-WiFi-SSIDs() { | |
return (netsh wlan show net mode=bssid) | |
} | |
function Is-Home() { | |
[bool]((Get-WiFi-SSIDs) -match "bhatia") | |
} | |
function Is-Office() { | |
[bool]((Get-WiFi-SSIDs) -match "DNET") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment