Skip to content

Instantly share code, notes, and snippets.

@perXautomatik
Forked from apfelchips/Profile.ps1
Last active October 4, 2022 22:41
Show Gist options
  • Save perXautomatik/a545e0bdcf5ff051592a4e4c1b644431 to your computer and use it in GitHub Desktop.
Save perXautomatik/a545e0bdcf5ff051592a4e4c1b644431 to your computer and use it in GitHub Desktop.
my crossplatform PowerShell Profile
# Auto detect text files and perform LF normalization
* text=auto
*/
Modules/*
[submodule "Snipps/EnvPath"]
path = Snipps/EnvPath
url = https://gist.github.com/2281de51f26338d8e273b0161785ed72

The Goods

PowerShell Profile

My PS Scripts

  • Add helper functions (uptime, reload-profile, find-file, unzip, and print-path)
  • Add equivalents for my favorite Unix commands (df, sed, sed-recursive, grep, grepv, which, export, pkill, pgrep, touch, sudo, pstree)
  • Add Git aliases (gc for git checkout, gp for git pull)
  • Set the default output to utf8
  • Increase the linme history to 10000 Get-FolderAccess screenshot: http://i.imgur.com/21LhwVy.jpg

Copy-File screenshot: http://imgur.com/hT8yoUm.jpg

# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# src: https://gist.github.com/apfelchips/62a71500a0f044477698da71634ab87b
# New-Item $(Split-Path "$($PROFILE.CurrentUserCurrentHost)") -ItemType Directory -ea 0; Invoke-WebRequest -Uri "https://git.io/JYZTu" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
# ref: https://devblogs.microsoft.com/powershell/optimizing-your-profile/#measure-script
# ref: Powershell $? https://stackoverflow.com/a/55362991
# ref: Write-* https://stackoverflow.com/a/38527767
# Write-Host wrapper for Write-Information -InformationAction Continue
# define these environment variables if not set already and also provide them as PSVariables
if ( ( $null -eq $PSVersionTable.PSEdition) -or ($PSVersionTable.PSEdition -eq "Desktop") ) { $PSVersionTable.PSEdition = "Desktop" ;$IsWindows = $true }
if ( -not $IsWindows ) { function Test-IsAdmin { if ( (id -u) -eq 0 ) { return $true } return $false } }
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Clean-Object {
process {
$_.PSObject.Properties.Remove('PSComputerName')
$_.PSObject.Properties.Remove('RunspaceId')
$_.PSObject.Properties.Remove('PSShowComputerName')
}
#Where-Object { $_.PSObject.Properties.Value -ne $null}
}
function Get-Environment { # Get-Variable to show all Powershell Variables accessible via $
if ( $args.Count -eq 0 ) {
Get-Childitem env:
} elseif( $args.Count -eq 1 ) {
Start-Process (Get-Command $args[0]).Source
} else {
Start-Process (Get-Command $args[0]).Source -ArgumentList $args[1..($args.Count-1)]
}
}
function cf {
if ( $null -ne $(Get-Module PSFzf) ) {
Get-ChildItem . -Recurse -Attributes Directory | Invoke-Fzf | Set-Location
} else {
Write-Error "please install PSFzf"
}
}
if ( $(Test-CommandExists 'git') ) {
Set-Alias g git -Option AllScope
function git-root { $gitrootdir = (git rev-parse --show-toplevel) ; if ( $gitrootdir ) { Set-Location $gitrootdir } }
if ( $IsWindows ) {
function git-sh {
if ( $args.Count -eq 0 ) { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") -l
} else { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") $args }
}
function git-bash {
if ( $args.Count -eq 0 ) {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l
} else {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") $args
}
}
function git-vim { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l -c `'vim $args`' }
if ( -Not (Test-CommandExists 'sh') ){ Set-Alias sh git-sh -Option AllScope }
if ( -Not (Test-CommandExists 'bash') ){ Set-Alias bash git-bash -Option AllScope }
if ( -Not (Test-CommandExists 'vi') ){ Set-Alias vi git-vim -Option AllScope }
if ( -Not (Test-CommandExists 'vim') ){ Set-Alias vim git-vim -Option AllScope }
}
}
function Select-Value { # src: https://geekeefy.wordpress.com/2017/06/26/selecting-objects-by-value-in-powershell/
[Cmdletbinding()]
param(
[parameter(Mandatory=$true)] [String] $Value,
[parameter(ValueFromPipeline=$true)] $InputObject
)
process {
# Identify the PropertyName for respective matching Value, in order to populate it Default Properties
$Property = ($PSItem.properties.Where({$_.Value -Like "$Value"})).Name
If ( $Property ) {
# Create Property a set which includes the 'DefaultPropertySet' and Property for the respective 'Value' matched
$DefaultPropertySet = $PSItem.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
$TypeName = ($PSItem.PSTypenames)[0]
Get-TypeData $TypeName | Remove-TypeData
Update-TypeData -TypeName $TypeName -DefaultDisplayPropertySet ($DefaultPropertySet+$Property |Select-Object -Unique)
$PSItem | Where-Object {$_.properties.Value -like "$Value"}
}
}
}
function pause($message="Press any key to continue . . . ") {
Write-Host -NoNewline $message
$i=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183
while ($null -eq $k.VirtualKeyCode -or $i -Contains $k.VirtualKeyCode){
$k = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Write-Host ""
}
# native touch implementation
# src: https://ss64.com/ps/syntax-touch.html
function Set-FileTime {
param(
[string[]]$paths,
[bool]$only_modification = $false,
[bool]$only_access = $false
)
begin {
function updateFileSystemInfo([System.IO.FileSystemInfo]$fsInfo) {
$datetime = Get-Date
if ( $only_access ) {
$fsInfo.LastAccessTime = $datetime
} elseif ( $only_modification ) {
$fsInfo.LastWriteTime = $datetime
} else {
$fsInfo.CreationTime = $datetime
$fsInfo.LastWriteTime = $datetime
$fsInfo.LastAccessTime = $datetime
}
}
function touchExistingFile($arg) {
if ( $arg -is [System.IO.FileSystemInfo] ) {
updateFileSystemInfo($arg)
} else {
$resolvedPaths = Resolve-Path $arg
foreach ($rpath in $resolvedPaths) {
if ( Test-Path -type Container $rpath ) {
$fsInfo = New-Object System.IO.DirectoryInfo($rpath)
} else {
$fsInfo = New-Object System.IO.FileInfo($rpath)
}
updateFileSystemInfo($fsInfo)
}
}
}
function touchNewFile([string]$path) {
#$null > $path
Set-Content -Path $path -value $null;
}
}
process {
if ( $_ ) {
if ( Test-Path $_ ) {
touchExistingFile($_)
} else {
touchNewFile($_)
}
}
}
end {
if ( $paths ) {
foreach ( $path in $paths ) {
if ( Test-Path $path ) {
touchExistingFile($path)
} else {
touchNewFile($path)
}
}
}
}
}
if ( $IsWindows ) {
function stree($directory = $pwd) {
$gitrootdir = (Invoke-Command{Set-Location $args[0]; git rev-parse --show-toplevel 2>&1;} -ArgumentList $directory)
if ( Test-Path -Path "$gitrootdir\.git" -PathType Container) {
$newestExe = Get-Item "${env:ProgramFiles(x86)}\Atlassian\SourceTree\SourceTree.exe" | select -Last 1
Write-Debug "Opening $gitrootdir with $newestExe"
Start-Process -filepath $newestExe -ArgumentList "-f `"$gitrootdir`" log"
} else {
Write-Error "git directory not found"
}
}
if ( "${env:ChocolateyInstall}" -eq "" ) {
function Install-Chocolatey {
if (Get-Command choco -ErrorAction SilentlyContinue) {
Write-Error "chocolatey already installed!"
} else {
Start-Process (Get-HostExecutable) -ArgumentList "-Command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1') -verb RunAs"
}
}
else {
function choco { Start-Process (Get-HostExecutable) -ArgumentList "-noProfile -noLogo -Command choco.exe ${args}; pause" -verb runAs }
}
}
}
function Get-HostExecutable {
if ( $PSVersionTable.PSEdition -eq "Core" ) {
$ConsoleHostExecutable = (get-command pwsh).Source
} else {
$ConsoleHostExecutable = (get-command powershell).Source
}
return $ConsoleHostExecutable
}
# don't override chocolatey sudo or unix sudo
if ( -not $(Test-CommandExists 'sudo') ) {
function sudo() {
if ( $args.Length -eq 0 ) {
Start-Process $(Get-HostExecutable) -verb "runAs"
} elseif ( $args.Length -eq 1 ) {
Start-Process $args[0] -verb "runAs"
} else {
Start-Process $args[0] -ArgumentList $args[1..$args.Length] -verb "runAs"
}
}
}
# Helper Functions
#######################################################
function uptimef {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function unzipf ($path) {
$dirname = (Get-Item $path).Basename
echo("Extracting", $path, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $path -OutputPath $dirname -ShowProgress
}
# already expanded to save time https://github.com/nvbn/thefuck/wiki/Shell-aliases#powershell
if ( $(Test-CommandExists 'thefuck') ) {
function fuck {
$PYTHONIOENCODING_BKP=$env:PYTHONIOENCODING
$env:PYTHONIOENCODING="utf-8"
$history = (Get-History -Count 1).CommandLine
if (-not [string]::IsNullOrWhiteSpace($history)) {
$fuck = $(thefuck $args $history)
if ( -not [string]::IsNullOrWhiteSpace($fuck) ) {
if ( $fuck.StartsWith("echo") ) { $fuck = $fuck.Substring(5) } else { iex "$fuck" }
}
}
[Console]::ResetColor()
$env:PYTHONIOENCODING=$PYTHONIOENCODING_BKP
}
Set-Alias f fuck -Option AllScope
}
# hacks for old powerhsell versions
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
function Get-ExitBoolean($command) { & $command | Out-Null; $?} ; Set-Alias geb Get-ExitBoolean # fixed: https://github.com/PowerShell/PowerShell/pull/9849
function Use-Default # $var = d $Value : "DefaultValue" eg. ternary # fixed: https://toastit.dev/2019/09/25/ternary-operator-powershell-7/
{
for ($i = 1; $i -lt $args.Count; $i++){
if ($args[$i] -eq ":"){
$coord = $i; break
}
}
if ($coord -eq 0) {
throw new System.Exception "No operator!"
}
if ($args[$coord - 1] -eq ""){
$toReturn = $args[$coord + 1]
} else {
$toReturn = $args[$coord -1]
}
return $toReturn
}
Set-Alias d Use-Default
}
if ( $IsWindows ) {
# src: http://serverfault.com/questions/95431
function Test-IsAdmin { $user = [Security.Principal.WindowsIdentity]::GetCurrent(); return $(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator); }
function Reopen-here { Get-Process explorer | Stop-Process Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Get-Process explorer | Stop-Process'" -verb "runAs"}
function Reset-Spooler { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Stop-Service -Name Spooler -Force; Get-Item ${env:SystemRoot}\System32\spool\PRINTERS\* | Remove-Item -Force -Recurse; Start-Service -Name Spooler'" -verb "runAs" }
function subl { Start-Process "${Env:ProgramFiles}\Sublime Text\subl.exe" -ArgumentList $args -WindowStyle Hidden } # hide subl shim script
function get-tempfilesNfolders { foreach ($folder in @('C:\Windows\Temp\*', 'C:\Documents and Settings\*\Local Settings\temp\*', 'C:\Users\*\Appdata\Local\Temp\*', 'C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*', 'C:\Windows\SoftwareDistribution\Download', 'C:\Windows\System32\FNTCACHE.DAT')) {$_} }
function Export-Regestrykey { param ( $reg = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\' ,$outFile = 'H:\hkcu-regbackup.txt' ) get-childitem -path $reg | out-file $outFile }
function start-bc ($REMOTE,$LOCAL,$BASE,$MERGED) { cmd /c "${Env:ProgramFiles}\BeondCompare4\BComp.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" } #todo: replace hardcode with global variable pointing to path
function start-cygwin { param ( $cygwinpath = "${Env:ProgramFiles}\cygwin64\" ) ."$cygwinpath\Cygwin.bat" }
function Compare-ImagesMetadata { param ( $exifToolPath = "${Env:ProgramFiles}\geosetter\tools\" ,$inputA = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front (2).jpg" ,$inputB = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front.jpg" ) ; $set1 = .\exiftool.exe -a -u -g1 $inputA ; $set2 = .\exiftool.exe -a -u -g1 $inputB ; Compare-Object $set1 $set2 | select -ExpandProperty inputobject }
function new-SymbolicLink { param ( $where = 'H:\mina grejer\Till Github' ,$from = 'H:\mina grejer\Project shelf\Till Github' ) New-Item -Path $where -ItemType SymbolicLink -Value $from }
}
if (Test-CommandExists 'search-Everything')
{
function invoke-Everything([string]$filter) { @(Search-Everything -filter $filter -global) }
function invoke-FuzzyWithEverything($searchstring) { menu @(everything "ext:exe $searchString") | %{& $_ } } #use whatpulse db first, then everything #todo: sort by rescent use #use everything to find executable for fast execution
function Every-execute ($inputx) { $filter = "ext:exe $inputx" ; $filter } #& (Every-Menu $filter)
function Every-AsHashMap { param( $filter = 'ext:psd1 \module') $q = @{} ; everything $filter | %{@{ name = (get -item $_).name ; time=(get -item $_).LastWriteTime ; path=(get -item $_) } } | sort -object -property time | %{ $q[$_.name] = $_.path } ; $q | select -property values}
function Every-execute { param( $filter = 'regex:".*\\data\\[^\\]*.ahk"',$navigate=$true) Every-Menu | % { if($navigate) {cd ($_ | split -path -parent)} ; . $_ } }
function Every-Load { param( $psFileFilter = 'convert-xlsx-to-csv.ps1') . ( everythnig $psFileFilter | select -first 1) } ; invoke-expression "ExcelToCsv -File 'D:\unsorted\fannyUtskick.xlsx'"
function Every-Explore { param( $filter = 'ext:exe lasso') ; Every-Menu $filter | % { $path = if(!( Test-Path $_ -PathType Container)) { $_ | split-path -leaf } else {$_} ; explorer $path } }
function Every-Menu { param( $filter) $a= @(everything $filter) ; if($a.count -eq 1) {$a} else {menu $a} }
}
if (Test-CommandExists 'git')
{ #todo: move to git aliases
function invoke-gitCheckout () { & git checkout $args }
function invoke-gitFetchOrig { git fetch origin }
Function invoke-GitLazy($path,$message) { cd $path ; git lazy $message } ;
Function invoke-GitLazySilently {Out-File -FilePath .\lazy.log -inputObject (invoke-GitLazy 'AutoCommit' 2>&1 )} ; #todo: parameterize #todo: rename to more descriptive #todo: breakout
function invoke-gitRemote { param ($subCommand = 'get-url',$name = "origin" ) git remote $subCommand $name }
Function invoke-GitSubmoduleAdd([string]$leaf,[string]$remote,[string]$branch) { git submodule add -f --name $leaf -- $remote $branch ; git commit -am $leaf+$remote+$branch } ; #todo: move to git aliases #Git Ad $leaf as submodule from $remote and branch $branch
}
if ( $null -ne $(Get-Module PSReadline -ea SilentlyContinue)) {
function find-historyAppendClipboard($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{ Set-Clipboard -Value $_ }} #search history of past expressions and adds to clipboard
function find-historyInvoke($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{Invoke-Expression $_ } } #search history of past expressions and invokes it, doesn't register the expression itself in history, but the pastDo expression.
}
function split-fileByLineNr { param( $pathName = '.\gron.csv',$OutputFilenamePattern = 'output_done_' , $LineLimit = 60) ;
$ext = $pathName | split-path -Extension
$inputx = Get-Content ;
$line = 0 ;
$i = 0 ;
$path = 0 ;
$start = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq $LineLimit -Or $line -eq $inputx.Length) {
$path++ ;
$pathname = "$OutputFilenamePattern$path$ext" ;
$inputx[$start..($line - 1)] | Out -File $pathname -Force ;
$start = $line ;
$i = 0 ;
Write-Host "$pathname" ;
} ;
$i++ ;
$line++
}
}
function split-fileByMatch($pathName , $regex) { #param( $pathName = 'C:\Users\crbk01\Documents\WindowsPowerShell\snipps\Modules\Todo SplitUp.psm1' , $regex = '(?<=function\s)[^\s\(]*') ;
$ext = ($pathName | split-path -Extension)
$parent = ($pathName | split-path -Parent)
$OriginalName = ($pathName | split-path -LeafBase)
$inputx = Get-Content $pathName; $line = 0 ; $i = 0 ; $start = @(select-string -path $pathName -pattern $regex ) | select linenumber ; $LineLimit = $start | select -Skip 1 ; $names = @() ; [regex]::matches($inputx,$regex).groups.value | %{$names+= $_ }
$occurence = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq ([int]$LineLimit[$occurence].linenumber -1) -Or $line -eq $inputx.Length)
{
$currentName = $names[$occurence];
$pathname = Join-Path -path $parent -childPath "$OriginalName-$currentName$ext" ;
$u = ([int]$start[$occurence].linenumber -1)
$inputx[$u..($line - 1)] > $pathname
$occurence++ ;
Write-Host "$u..($line - 1)$pathname" ;
};
$i++ ;
$line++
}
}
#function aliasCode { & $env:code }
function .. { Set-Location ".." }
function .... { Set-Location (Join-Path -Path ".." -ChildPath "..") }
function all { process { $_ | Select-Object * } } # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#piping-objects-to-functions
function cdc { Set-Location "$XDG_CONFIG_HOME" }
function cdcheat { Set-Location "$CHEATS_DIR" }
function cdd { Set-Location "$DESKTOP_DIR" }
function cddd { Set-Location "$DEVEL_DIR" }
function cddev { Set-Location "$DEVEL_DIR" }
function cdn { Set-Location "$NOTES_DIR" }
function cdports { Set-Location "$PORTS_DIR" }
function cdt { Set-Location "$TODO_DIR" }
function clear-Days_Back { param( $path = "C:\Support\SQLBac\" ,$Daysback = "0" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item }
function ConvertFrom-Bytes { param( [string]$bytes, [string]$savepath ) $dir = Split-Path $savepath if (!(Test-Path $dir)) { md $dir | Out-Null } [convert]::FromBase64String($bytes) | Set-Content $savepath -Encoding Byte }
function ConvertTo-Bytes ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } [convert]::ToBase64String((Get-Content $path -Encoding Byte)) }
function df { get-volume }
function enter-dir { param( $path = '%USERPROFILE%\Desktop\' ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Set-Location $Path }; #if no param, navigate to desktop # 5. 更改工作目录 # 输入要切换到的路径 # 用法示例:cd C:/ # 默认路径:D 盘的桌面
function exit-Nrenter { shutdown /r } #reboot
function export($name, $value) { set-item -force -path "env:$name" -value $value; }
function foldercontain($folder,$name) { $q = get-childitem $folder; return $q -contains $name }
function Get-AllNic { Get-NetAdapter | Sort-Object -Property MacAddress } # 1. 获取所有 Network Interface
function get-Childnames { param( $path = $pwd) (Get-ChildItem -path $path).Name ; Write-Host("") }; # 3. 查看目录 ls & ll
function Get-DefaultAliases { Get-Alias | Where-Object { $_.Options -match "ReadOnly" }}
function get-envVar { Get-Childitem -Path Env:*}
function get-EspHeader { param( $path = '.\FormiD.esp', $max = 13,$headerSize = 3 ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $q = ((( -join (gc $path | select -first $headerSize)) -replace "[^\w. ] {1,}","å").split("å") -match "^.*[.](esp |esm)$") ; $q | ? {($q | select -first $max) -NotContains $_}}
function get-historyPath { (Get-PSReadlineOption).HistorySavePath }
function Get-IPv4Routes { Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } # 2. 获取 IPv4 关键路由
function Get-IPv6Routes { Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } # 3. 获取 IPv6 关键路由
function get-isFolder {$PSBoundParameters -is [system.in.folder]}
function get-parameters { Get-Member -Parameter *}
function get-RegInstallpaths { Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | % { Get -ItemProperty $_.PsPath } | Select DisplayName,InstallLocation }
function get-whatpulse { param( $program,$path) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $query = "select rightstr(path,instr(reverse(path),'/') -1) exe,path from (select max(path) path,max(cast(replace(version,'.','') as integer)) version from applications group by case when online_app_id = 0 then name else online_app_id end)" ; $adapter = newSqliteConnection -source (Everything 'whatpulse.db')[0] -query $query ; $b=@($data.item('exe')) ; $a = @($data.item('path')) ; $i=0 ; while($i -lt $a.Length) {$res[$b[$i]]=$a[$i] ; $i++ } ; $res | where { $_.name -match $program -and $_.path -match $path}}
function grep { process { $_ | Select-String -Pattern $args } } # function grep($regex, $dir) { if ( $dir ) { ls $dir | select-string $regex return } $input | select-string $regex }
function grepv($regex) { $input | ? { !$_.Contains($regex) } }
function Initialize-Profile { . $PROFILE.CurrentUserCurrentHost} #function initialize-profile { & $profile } #reload-profile is an unapproved verb.
function invoke-Nmake { nmake.exe $args -nologo }; # 1. 编译函数 make
function invoke-powershellAsAdmin { Start-Process powershell -Verb runAs } #new ps OpenAsADmin
function join-ByRuncunfig { param( $prefix='[$]APPLICATION_CONFIG_DIR[$][/]'
,$refixReplace='C:\Users\crbk01\AppData\Roaming\JetBrains\DataGrip2021.1\',
$runconfig="TillMinaMedelanden.run.xml",$output='runConfig/Combined.sql') [xml]$xml=get -content $runConfig -Encoding UTF8 ; $xml.component.configuration.'script -file' | %
{$_.value -replace $prefix, ($prefixReplace -replace '\\','/')} | % {" - -:$_"
; get -content -path $_ -Encoding UTF8 ; "go" } >> $output }
function list { process { $_ | Format-List * } } # fl is there by default
function man { Get-Help $args[0] | out-host -paging }
function md { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function measure-ExtOccurenseRecursivly { param( $path = "D:\Project Shelf\MapBasic" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Get-ChildItem -Path $path -Recurse -File | group Extension -NoElement | sort Count -Descending | select -Property name }
function measure-words { param( $inputStream = (Get -ChildItem | % { Get -Content $_.name }), $regex="[^a-z0-9_-]") $hash = @{} ; $a="" ; $inputStream | % {$a+=$_} ; $a -split $regex | % { $_.tolower()} | % {$hash[$_]++} ; $sorted= % {$hash.GetEnumerator() | sort -object {[int]$_.value}} ; return $sorted} ; $sorted | where{$_.name -notmatch "^\d+$"} | where{$_.name.length -gt 4 }
function mkdir { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function My-Scripts { Get-Command -CommandType externalscript }
function open-here { param( $Path = $pwd ) Invoke-Item $Path }; # 4. 打开当前工作目录 # 输入要打开的路径 # 用法示例:open C:\ # 默认路径:当前工作文件夹
function open-ProfileFolder { explorer (split-path -path $profile -parent)}
function pgrep($name) { Get-Process $name }
function pkill($name) { Get-Process $name -ErrorAction SilentlyContinue | kill }
function pull () { & get pull $args }
function read-aliases { Get-Alias | Where-Object { $_.Options -notmatch "ReadOnly" }}
function read-EnvPaths { ($Env:Path).Split(";") }
function read-headOfFile { param( $linr = 10, $path ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } gc -Path $path -TotalCount $linr }
function read-json { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSCustomObject] $input ) $json = [ordered]@{}; ($input).PSObject.Properties | % { $json[$_.Name] = $_.Value } $json.SyncRoot }
function read-paramNaliases ($command) { (Get-Command $command).parameters.values | select name, @{n='aliases';e={$_.aliases}} }
function read-pathsAsStream { get-childitem | out-string -stream } # filesInFolAsStream ;
function read-uptime { Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime'; EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}} } #doesn't psreadline module implement this already?
function Remove-CustomAliases { Get-Alias | Where-Object { ! $_.Options -match "ReadOnly" } | % { Remove-Item alias:$_ }} # https://stackoverflow.com/a/2816523
function remove-TempfilesNfolders { foreach ($folder in get-tempfilesNfolders) {Remove-Item $folder -force -recurse} }
function Sanatize-path { param( $path='G:\mhk',$replaceChar='_') if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $regex= "[^\)\w \\\ -:.åäöÅÄÖ %\ {,~$\(!@¤£é&=#§]" ; Get -ChildItem $path -Recurse | Where -Object {$_.name -match $regex} | rename -item -newName {$_.name -replace $regex,$replaceChar}}
function sed($path, $find, $replace) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } (Get-Content $path).replace("$find", $replace) | Set-Content $path }
function set-FileEncodingUtf8 ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } sc $path -encoding utf8 -value(gc $path) }
function set-x { Set-PSDebug -trace 2}
function set+x { Set-PSDebug -trace 0}
function sort-PathByLvl { param( $inputList) $inputList | Sort {($_ -split '\\').Count}, {$_} -Descending | select -object -first 2 | % { $error.clear() ; try { out -null -input (test -ModuleManifest $_ > '&2>&1' ) } catch { "Error" } ; if (!$error) { $_ } }}
function start-BrowserFlags { vivaldi "vivaldi://flags" } #todo: use standard browser instead of hardcoded
function start-notepad-profile { notepad $profile}
function string { process { $_ | Out-String -Stream } }
function touch($path) { "" | Out-File $path -Encoding ASCII }
function which($name) { Get-Command $name | Select-Object -ExpandProperty Definition } #should use more
Remove-Item alias:ls -ea SilentlyContinue ; function ls { Get-Childitem} # ls -al is musclememory by now so ignore all args for this "alias"
#------------------------------- Functions END -------------------------------
#------------------------------- Set alias BEGIN -------------------------------
$TAType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators") ; $TAType::Add('accelerators',$TAType)
set-alias accelerators [accelerators]::Get
set-alias edprofile start-Notepad-Profile
set-alias exp-pro open-ProfileFolder
set-alias kidStream get-childitem | out-string -stream
set-alias history (Get-PSReadlineOption).HistorySavePath
set-alias parameters get-parameters
set-alias start-su start-powershellAsAdmin
set-alias version $PSVersionTable # bash-like
Set-Alias cat Get-Content -Option AllScope
Set-Alias cd Set-Location -Option AllScope
Set-Alias clear Clear-Host -Option AllScope
Set-Alias cp Copy-Item -Option AllScope
Set-Alias history Get-History -Option AllScope
Set-Alias kill Stop-Process -Option AllScope
Set-Alias lp Out-Printer -Option AllScope
Set-Alias mv Move-Item -Option AllScope
Set-Alias ps Get-Process -Option AllScope
Set-Alias pwd Get-Location -Option AllScope
Set-Alias which Get-Command -Option AllScope
Set-Alias open Invoke-Item -Option AllScope
Set-Alias basename Split-Path -Option AllScope
Set-Alias realpath Resolve-Path -Option AllScope # cmd-like Set-Alias rm Remove-Item -Option AllScope
Set-Alias rmdir Remove-Item -Option AllScope
Set-Alias echo Write-Output -Option AllScope
Set-Alias cls Clear-Host -Option AllScope
Set-Alias chdir Set-Location -Option AllScope
Set-Alias copy Copy-Item -Option AllScope
Set-Alias del Remove-Item -Option AllScope
Set-Alias dir Get-Childitem -Option AllScope
Set-Alias erase Remove-Item -Option AllScope
Set-Alias move Move-Item -Option AllScope
Set-Alias rd Remove-Item -Option AllScope
Set-Alias ren Rename-Item -Option AllScope
Set-Alias set Set-Variable -Option AllScope
Set-Alias type Get-Content -Option AllScope
Set-Alias env Get-Environment -Option AllScope # custom aliases
Set-Alias flush-dns Clear-DnsClientCache -Option AllScope
Set-Alias touch Set-FileTime -Option AllScope
set-alias lsx get-Childnames -Option AllScope
set-alias filesinfolasstream read-childrenAsStream -Option AllScope
set-alias bcompare start-bc -Option AllScope
set-alias GitAdEPathAsSNB invoke-GitSubmoduleAdd -Option AllScope
set-alias GitUp invoke-GitLazy -Option AllScope
set-alias gitSilently invoke-GitLazySilently -Option AllScope
set-alias gitSingleRemote invoke-gitFetchOrig -Option AllScope
set-alias executeThis invoke-FuzzyWithEverything -Option AllScope
set-alias filesinfolasstream read-pathsAsStream -Option AllScope
set-alias everything invoke-Everything -Option AllScope
set-alias make invoke-Nmake -Option AllScope
set-alias MyAliases read-aliases -Option AllScope
set-alias OpenAsADmin invoke-powershellAsAdmin -Option AllScope
set-alias home open-here -Option AllScope
set-alias pastDo find-historyInvoke -Option AllScope
set-alias pastDoEdit find-historyAppendClipboard -Option AllScope
set-alias HistoryPath (Get-PSReadlineOption).HistorySavePath -Option AllScope
set-alias reboot exit-Nrenter -Option AllScope
set-alias browserflags start-BrowserFlags -Option AllScope
set-alias df get-volume -Option AllScope
set-alias printpaths read-EnvPaths -Option AllScope
set-alias reload initialize-profile -Option AllScope
set-alias uptime read-uptime -Option AllScope
set-alias getnic get-mac -Option AllScope # 1. 获取所有 Network Interface set-alias ll Get-ChildItem -Option AllScope
set-alias getip Get-IPv4Routes -Option AllScope
set-alias getip6 Get-IPv6Routes -Option AllScope
set-alias os-update Update-Packages -Option AllScope
set-alias remote invoke-gitRemote -Option AllScope
set-alias gitsplit subtree-split-rm-commit -Option AllScope
set-alias new-name Rename-Item -Option AllScope
set-alias set-name Rename-Item -Option AllScope
set-alias isFolder get-isFolder -Option AllScope
set-alias start-powershellAsAdmin invoke-powershellAsAdmin -Option AllScope
set-alias psVersion $PSVersionTable.PSVersion.Major -Option AllScope
#------------------------------- Set alias END -------------------------------
Write-Host "PSVersion: $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
Write-Host "PSEdition: $($PSVersionTable.PSEdition)"
Write-Host "Profile: $PSCommandPath"
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# src: https://gist.github.com/apfelchips/62a71500a0f044477698da71634ab87b
# New-Item $(Split-Path "$($PROFILE.CurrentUserCurrentHost)") -ItemType Directory -ea 0; Invoke-WebRequest -Uri "https://git.io/JYZTu" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
# ref: https://devblogs.microsoft.com/powershell/optimizing-your-profile/#measure-script
# ref: Powershell $? https://stackoverflow.com/a/55362991
# ref: Write-* https://stackoverflow.com/a/38527767
# Write-Host wrapper for Write-Information -InformationAction Continue
# define these environment variables if not set already and also provide them as PSVariables
if ( ( $null -eq $PSVersionTable.PSEdition) -or ($PSVersionTable.PSEdition -eq "Desktop") ) { $PSVersionTable.PSEdition = "Desktop" ;$IsWindows = $true }
if ( -not $IsWindows ) { function Test-IsAdmin { if ( (id -u) -eq 0 ) { return $true } return $false } }
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Clean-Object {
process {
$_.PSObject.Properties.Remove('PSComputerName')
$_.PSObject.Properties.Remove('RunspaceId')
$_.PSObject.Properties.Remove('PSShowComputerName')
}
#Where-Object { $_.PSObject.Properties.Value -ne $null}
}
function Get-Environment { # Get-Variable to show all Powershell Variables accessible via $
if ( $args.Count -eq 0 ) {
Get-Childitem env:
} elseif( $args.Count -eq 1 ) {
Start-Process (Get-Command $args[0]).Source
} else {
Start-Process (Get-Command $args[0]).Source -ArgumentList $args[1..($args.Count-1)]
}
}
function cf {
if ( $null -ne $(Get-Module PSFzf) ) {
Get-ChildItem . -Recurse -Attributes Directory | Invoke-Fzf | Set-Location
} else {
Write-Error "please install PSFzf"
}
}
if ( $(Test-CommandExists 'git') ) {
Set-Alias g git -Option AllScope
function git-root { $gitrootdir = (git rev-parse --show-toplevel) ; if ( $gitrootdir ) { Set-Location $gitrootdir } }
if ( $IsWindows ) {
function git-sh {
if ( $args.Count -eq 0 ) { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") -l
} else { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") $args }
}
function git-bash {
if ( $args.Count -eq 0 ) {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l
} else {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") $args
}
}
function git-vim { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l -c `'vim $args`' }
if ( -Not (Test-CommandExists 'sh') ){ Set-Alias sh git-sh -Option AllScope }
if ( -Not (Test-CommandExists 'bash') ){ Set-Alias bash git-bash -Option AllScope }
if ( -Not (Test-CommandExists 'vi') ){ Set-Alias vi git-vim -Option AllScope }
if ( -Not (Test-CommandExists 'vim') ){ Set-Alias vim git-vim -Option AllScope }
}
}
function Select-Value { # src: https://geekeefy.wordpress.com/2017/06/26/selecting-objects-by-value-in-powershell/
[Cmdletbinding()]
param(
[parameter(Mandatory=$true)] [String] $Value,
[parameter(ValueFromPipeline=$true)] $InputObject
)
process {
# Identify the PropertyName for respective matching Value, in order to populate it Default Properties
$Property = ($PSItem.properties.Where({$_.Value -Like "$Value"})).Name
If ( $Property ) {
# Create Property a set which includes the 'DefaultPropertySet' and Property for the respective 'Value' matched
$DefaultPropertySet = $PSItem.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
$TypeName = ($PSItem.PSTypenames)[0]
Get-TypeData $TypeName | Remove-TypeData
Update-TypeData -TypeName $TypeName -DefaultDisplayPropertySet ($DefaultPropertySet+$Property |Select-Object -Unique)
$PSItem | Where-Object {$_.properties.Value -like "$Value"}
}
}
}
function pause($message="Press any key to continue . . . ") {
Write-Host -NoNewline $message
$i=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183
while ($null -eq $k.VirtualKeyCode -or $i -Contains $k.VirtualKeyCode){
$k = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Write-Host ""
}
# native touch implementation
# src: https://ss64.com/ps/syntax-touch.html
function Set-FileTime {
param(
[string[]]$paths,
[bool]$only_modification = $false,
[bool]$only_access = $false
)
begin {
function updateFileSystemInfo([System.IO.FileSystemInfo]$fsInfo) {
$datetime = Get-Date
if ( $only_access ) {
$fsInfo.LastAccessTime = $datetime
} elseif ( $only_modification ) {
$fsInfo.LastWriteTime = $datetime
} else {
$fsInfo.CreationTime = $datetime
$fsInfo.LastWriteTime = $datetime
$fsInfo.LastAccessTime = $datetime
}
}
function touchExistingFile($arg) {
if ( $arg -is [System.IO.FileSystemInfo] ) {
updateFileSystemInfo($arg)
} else {
$resolvedPaths = Resolve-Path $arg
foreach ($rpath in $resolvedPaths) {
if ( Test-Path -type Container $rpath ) {
$fsInfo = New-Object System.IO.DirectoryInfo($rpath)
} else {
$fsInfo = New-Object System.IO.FileInfo($rpath)
}
updateFileSystemInfo($fsInfo)
}
}
}
function touchNewFile([string]$path) {
#$null > $path
Set-Content -Path $path -value $null;
}
}
process {
if ( $_ ) {
if ( Test-Path $_ ) {
touchExistingFile($_)
} else {
touchNewFile($_)
}
}
}
end {
if ( $paths ) {
foreach ( $path in $paths ) {
if ( Test-Path $path ) {
touchExistingFile($path)
} else {
touchNewFile($path)
}
}
}
}
}
if ( $IsWindows ) {
function stree($directory = $pwd) {
$gitrootdir = (Invoke-Command{Set-Location $args[0]; git rev-parse --show-toplevel 2>&1;} -ArgumentList $directory)
if ( Test-Path -Path "$gitrootdir\.git" -PathType Container) {
$newestExe = Get-Item "${env:ProgramFiles(x86)}\Atlassian\SourceTree\SourceTree.exe" | select -Last 1
Write-Debug "Opening $gitrootdir with $newestExe"
Start-Process -filepath $newestExe -ArgumentList "-f `"$gitrootdir`" log"
} else {
Write-Error "git directory not found"
}
}
if ( "${env:ChocolateyInstall}" -eq "" ) {
function Install-Chocolatey {
if (Get-Command choco -ErrorAction SilentlyContinue) {
Write-Error "chocolatey already installed!"
} else {
Start-Process (Get-HostExecutable) -ArgumentList "-Command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1') -verb RunAs"
}
}
else {
function choco { Start-Process (Get-HostExecutable) -ArgumentList "-noProfile -noLogo -Command choco.exe ${args}; pause" -verb runAs }
}
}
}
function Get-HostExecutable {
if ( $PSVersionTable.PSEdition -eq "Core" ) {
$ConsoleHostExecutable = (get-command pwsh).Source
} else {
$ConsoleHostExecutable = (get-command powershell).Source
}
return $ConsoleHostExecutable
}
# don't override chocolatey sudo or unix sudo
if ( -not $(Test-CommandExists 'sudo') ) {
function sudo() {
if ( $args.Length -eq 0 ) {
Start-Process $(Get-HostExecutable) -verb "runAs"
} elseif ( $args.Length -eq 1 ) {
Start-Process $args[0] -verb "runAs"
} else {
Start-Process $args[0] -ArgumentList $args[1..$args.Length] -verb "runAs"
}
}
}
# Helper Functions
#######################################################
function uptimef {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function unzipf ($path) {
$dirname = (Get-Item $path).Basename
echo("Extracting", $path, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $path -OutputPath $dirname -ShowProgress
}
# already expanded to save time https://github.com/nvbn/thefuck/wiki/Shell-aliases#powershell
if ( $(Test-CommandExists 'thefuck') ) {
function fuck {
$PYTHONIOENCODING_BKP=$env:PYTHONIOENCODING
$env:PYTHONIOENCODING="utf-8"
$history = (Get-History -Count 1).CommandLine
if (-not [string]::IsNullOrWhiteSpace($history)) {
$fuck = $(thefuck $args $history)
if ( -not [string]::IsNullOrWhiteSpace($fuck) ) {
if ( $fuck.StartsWith("echo") ) { $fuck = $fuck.Substring(5) } else { iex "$fuck" }
}
}
[Console]::ResetColor()
$env:PYTHONIOENCODING=$PYTHONIOENCODING_BKP
}
Set-Alias f fuck -Option AllScope
}
# hacks for old powerhsell versions
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
function Get-ExitBoolean($command) { & $command | Out-Null; $?} ; Set-Alias geb Get-ExitBoolean # fixed: https://github.com/PowerShell/PowerShell/pull/9849
function Use-Default # $var = d $Value : "DefaultValue" eg. ternary # fixed: https://toastit.dev/2019/09/25/ternary-operator-powershell-7/
{
for ($i = 1; $i -lt $args.Count; $i++){
if ($args[$i] -eq ":"){
$coord = $i; break
}
}
if ($coord -eq 0) {
throw new System.Exception "No operator!"
}
if ($args[$coord - 1] -eq ""){
$toReturn = $args[$coord + 1]
} else {
$toReturn = $args[$coord -1]
}
return $toReturn
}
Set-Alias d Use-Default
}
if ( $IsWindows ) {
# src: http://serverfault.com/questions/95431
function Test-IsAdmin { $user = [Security.Principal.WindowsIdentity]::GetCurrent(); return $(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator); }
function Reopen-here { Get-Process explorer | Stop-Process Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Get-Process explorer | Stop-Process'" -verb "runAs"}
function Reset-Spooler { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Stop-Service -Name Spooler -Force; Get-Item ${env:SystemRoot}\System32\spool\PRINTERS\* | Remove-Item -Force -Recurse; Start-Service -Name Spooler'" -verb "runAs" }
function subl { Start-Process "${Env:ProgramFiles}\Sublime Text\subl.exe" -ArgumentList $args -WindowStyle Hidden } # hide subl shim script
function get-tempfilesNfolders { foreach ($folder in @('C:\Windows\Temp\*', 'C:\Documents and Settings\*\Local Settings\temp\*', 'C:\Users\*\Appdata\Local\Temp\*', 'C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*', 'C:\Windows\SoftwareDistribution\Download', 'C:\Windows\System32\FNTCACHE.DAT')) {$_} }
function Export-Regestrykey { param ( $reg = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\' ,$outFile = 'H:\hkcu-regbackup.txt' ) get-childitem -path $reg | out-file $outFile }
function start-bc ($REMOTE,$LOCAL,$BASE,$MERGED) { cmd /c "${Env:ProgramFiles}\BeondCompare4\BComp.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" } #todo: replace hardcode with global variable pointing to path
function start-cygwin { param ( $cygwinpath = "${Env:ProgramFiles}\cygwin64\" ) ."$cygwinpath\Cygwin.bat" }
function Compare-ImagesMetadata { param ( $exifToolPath = "${Env:ProgramFiles}\geosetter\tools\" ,$inputA = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front (2).jpg" ,$inputB = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front.jpg" ) ; $set1 = .\exiftool.exe -a -u -g1 $inputA ; $set2 = .\exiftool.exe -a -u -g1 $inputB ; Compare-Object $set1 $set2 | select -ExpandProperty inputobject }
function new-SymbolicLink { param ( $where = 'H:\mina grejer\Till Github' ,$from = 'H:\mina grejer\Project shelf\Till Github' ) New-Item -Path $where -ItemType SymbolicLink -Value $from }
}
if (Test-CommandExists 'search-Everything')
{
function invoke-Everything([string]$filter) { @(Search-Everything -filter $filter -global) }
function invoke-FuzzyWithEverything($searchstring) { menu @(everything "ext:exe $searchString") | %{& $_ } } #use whatpulse db first, then everything #todo: sort by rescent use #use everything to find executable for fast execution
function Every-execute ($inputx) { $filter = "ext:exe $inputx" ; $filter } #& (Every-Menu $filter)
function Every-AsHashMap { param( $filter = 'ext:psd1 \module') $q = @{} ; everything $filter | %{@{ name = (get -item $_).name ; time=(get -item $_).LastWriteTime ; path=(get -item $_) } } | sort -object -property time | %{ $q[$_.name] = $_.path } ; $q | select -property values}
function Every-execute { param( $filter = 'regex:".*\\data\\[^\\]*.ahk"',$navigate=$true) Every-Menu | % { if($navigate) {cd ($_ | split -path -parent)} ; . $_ } }
function Every-Load { param( $psFileFilter = 'convert-xlsx-to-csv.ps1') . ( everythnig $psFileFilter | select -first 1) } ; invoke-expression "ExcelToCsv -File 'D:\unsorted\fannyUtskick.xlsx'"
function Every-Explore { param( $filter = 'ext:exe lasso') ; Every-Menu $filter | % { $path = if(!( Test-Path $_ -PathType Container)) { $_ | split-path -leaf } else {$_} ; explorer $path } }
function Every-Menu { param( $filter) $a= @(everything $filter) ; if($a.count -eq 1) {$a} else {menu $a} }
}
if (Test-CommandExists 'git')
{ #todo: move to git aliases
function invoke-gitCheckout () { & git checkout $args }
function invoke-gitFetchOrig { git fetch origin }
Function invoke-GitLazy($path,$message) { cd $path ; git lazy $message } ;
Function invoke-GitLazySilently {Out-File -FilePath .\lazy.log -inputObject (invoke-GitLazy 'AutoCommit' 2>&1 )} ; #todo: parameterize #todo: rename to more descriptive #todo: breakout
function invoke-gitRemote { param ($subCommand = 'get-url',$name = "origin" ) git remote $subCommand $name }
Function invoke-GitSubmoduleAdd([string]$leaf,[string]$remote,[string]$branch) { git submodule add -f --name $leaf -- $remote $branch ; git commit -am $leaf+$remote+$branch } ; #todo: move to git aliases #Git Ad $leaf as submodule from $remote and branch $branch
}
if ( $null -ne $(Get-Module PSReadline -ea SilentlyContinue)) {
function find-historyAppendClipboard($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{ Set-Clipboard -Value $_ }} #search history of past expressions and adds to clipboard
function find-historyInvoke($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{Invoke-Expression $_ } } #search history of past expressions and invokes it, doesn't register the expression itself in history, but the pastDo expression.
}
function split-fileByLineNr { param( $pathName = '.\gron.csv',$OutputFilenamePattern = 'output_done_' , $LineLimit = 60) ;
$ext = $pathName | split-path -Extension
$inputx = Get-Content ;
$line = 0 ;
$i = 0 ;
$path = 0 ;
$start = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq $LineLimit -Or $line -eq $inputx.Length) {
$path++ ;
$pathname = "$OutputFilenamePattern$path$ext" ;
$inputx[$start..($line - 1)] | Out -File $pathname -Force ;
$start = $line ;
$i = 0 ;
Write-Host "$pathname" ;
} ;
$i++ ;
$line++
}
}
function split-fileByMatch($pathName , $regex) { #param( $pathName = 'C:\Users\crbk01\Documents\WindowsPowerShell\snipps\Modules\Todo SplitUp.psm1' , $regex = '(?<=function\s)[^\s\(]*') ;
$ext = ($pathName | split-path -Extension)
$parent = ($pathName | split-path -Parent)
$OriginalName = ($pathName | split-path -LeafBase)
$inputx = Get-Content $pathName; $line = 0 ; $i = 0 ; $start = @(select-string -path $pathName -pattern $regex ) | select linenumber ; $LineLimit = $start | select -Skip 1 ; $names = @() ; [regex]::matches($inputx,$regex).groups.value | %{$names+= $_ }
$occurence = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq ([int]$LineLimit[$occurence].linenumber -1) -Or $line -eq $inputx.Length)
{
$currentName = $names[$occurence];
$pathname = Join-Path -path $parent -childPath "$OriginalName-$currentName$ext" ;
$u = ([int]$start[$occurence].linenumber -1)
$inputx[$u..($line - 1)] > $pathname
$occurence++ ;
Write-Host "$u..($line - 1)$pathname" ;
};
$i++ ;
$line++
}
}
#function aliasCode { & $env:code }
function .. { Set-Location ".." }
function .... { Set-Location (Join-Path -Path ".." -ChildPath "..") }
function all { process { $_ | Select-Object * } } # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#piping-objects-to-functions
function cdc { Set-Location "$XDG_CONFIG_HOME" }
function cdcheat { Set-Location "$CHEATS_DIR" }
function cdd { Set-Location "$DESKTOP_DIR" }
function cddd { Set-Location "$DEVEL_DIR" }
function cddev { Set-Location "$DEVEL_DIR" }
function cdn { Set-Location "$NOTES_DIR" }
function cdports { Set-Location "$PORTS_DIR" }
function cdt { Set-Location "$TODO_DIR" }
function clear-Days_Back { param( $path = "C:\Support\SQLBac\" ,$Daysback = "0" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item }
function ConvertFrom-Bytes { param( [string]$bytes, [string]$savepath ) $dir = Split-Path $savepath if (!(Test-Path $dir)) { md $dir | Out-Null } [convert]::FromBase64String($bytes) | Set-Content $savepath -Encoding Byte }
function ConvertTo-Bytes ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } [convert]::ToBase64String((Get-Content $path -Encoding Byte)) }
function df { get-volume }
function enter-dir { param( $path = '%USERPROFILE%\Desktop\' ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Set-Location $Path }; #if no param, navigate to desktop # 5. 更改工作目录 # 输入要切换到的路径 # 用法示例:cd C:/ # 默认路径:D 盘的桌面
function exit-Nrenter { shutdown /r } #reboot
function export($name, $value) { set-item -force -path "env:$name" -value $value; }
function foldercontain($folder,$name) { $q = get-childitem $folder; return $q -contains $name }
function Get-AllNic { Get-NetAdapter | Sort-Object -Property MacAddress } # 1. 获取所有 Network Interface
function get-Childnames { param( $path = $pwd) (Get-ChildItem -path $path).Name ; Write-Host("") }; # 3. 查看目录 ls & ll
function Get-DefaultAliases { Get-Alias | Where-Object { $_.Options -match "ReadOnly" }}
function get-envVar { Get-Childitem -Path Env:*}
function get-EspHeader { param( $path = '.\FormiD.esp', $max = 13,$headerSize = 3 ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $q = ((( -join (gc $path | select -first $headerSize)) -replace "[^\w. ] {1,}","å").split("å") -match "^.*[.](esp |esm)$") ; $q | ? {($q | select -first $max) -NotContains $_}}
function get-historyPath { (Get-PSReadlineOption).HistorySavePath }
function Get-IPv4Routes { Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } # 2. 获取 IPv4 关键路由
function Get-IPv6Routes { Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } # 3. 获取 IPv6 关键路由
function get-isFolder {$PSBoundParameters -is [system.in.folder]}
function get-parameters { Get-Member -Parameter *}
function get-RegInstallpaths { Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | % { Get -ItemProperty $_.PsPath } | Select DisplayName,InstallLocation }
function get-whatpulse { param( $program,$path) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $query = "select rightstr(path,instr(reverse(path),'/') -1) exe,path from (select max(path) path,max(cast(replace(version,'.','') as integer)) version from applications group by case when online_app_id = 0 then name else online_app_id end)" ; $adapter = newSqliteConnection -source (Everything 'whatpulse.db')[0] -query $query ; $b=@($data.item('exe')) ; $a = @($data.item('path')) ; $i=0 ; while($i -lt $a.Length) {$res[$b[$i]]=$a[$i] ; $i++ } ; $res | where { $_.name -match $program -and $_.path -match $path}}
function grep { process { $_ | Select-String -Pattern $args } } # function grep($regex, $dir) { if ( $dir ) { ls $dir | select-string $regex return } $input | select-string $regex }
function grepv($regex) { $input | ? { !$_.Contains($regex) } }
function Initialize-Profile { . $PROFILE.CurrentUserCurrentHost} #function initialize-profile { & $profile } #reload-profile is an unapproved verb.
function invoke-Nmake { nmake.exe $args -nologo }; # 1. 编译函数 make
function invoke-powershellAsAdmin { Start-Process powershell -Verb runAs } #new ps OpenAsADmin
function join-ByRuncunfig { param( $prefix='[$]APPLICATION_CONFIG_DIR[$][/]'
,$refixReplace='C:\Users\crbk01\AppData\Roaming\JetBrains\DataGrip2021.1\',
$runconfig="TillMinaMedelanden.run.xml",$output='runConfig/Combined.sql') [xml]$xml=get -content $runConfig -Encoding UTF8 ; $xml.component.configuration.'script -file' | %
{$_.value -replace $prefix, ($prefixReplace -replace '\\','/')} | % {" - -:$_"
; get -content -path $_ -Encoding UTF8 ; "go" } >> $output }
function list { process { $_ | Format-List * } } # fl is there by default
function man { Get-Help $args[0] | out-host -paging }
function md { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function measure-ExtOccurenseRecursivly { param( $path = "D:\Project Shelf\MapBasic" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Get-ChildItem -Path $path -Recurse -File | group Extension -NoElement | sort Count -Descending | select -Property name }
function measure-words { param( $inputStream = (Get -ChildItem | % { Get -Content $_.name }), $regex="[^a-z0-9_-]") $hash = @{} ; $a="" ; $inputStream | % {$a+=$_} ; $a -split $regex | % { $_.tolower()} | % {$hash[$_]++} ; $sorted= % {$hash.GetEnumerator() | sort -object {[int]$_.value}} ; return $sorted} ; $sorted | where{$_.name -notmatch "^\d+$"} | where{$_.name.length -gt 4 }
function mkdir { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function My-Scripts { Get-Command -CommandType externalscript }
function open-here { param( $Path = $pwd ) Invoke-Item $Path }; # 4. 打开当前工作目录 # 输入要打开的路径 # 用法示例:open C:\ # 默认路径:当前工作文件夹
function open-ProfileFolder { explorer (split-path -path $profile -parent)}
function pgrep($name) { Get-Process $name }
function pkill($name) { Get-Process $name -ErrorAction SilentlyContinue | kill }
function pull () { & get pull $args }
function read-aliases { Get-Alias | Where-Object { $_.Options -notmatch "ReadOnly" }}
function read-EnvPaths { ($Env:Path).Split(";") }
function read-headOfFile { param( $linr = 10, $path ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } gc -Path $path -TotalCount $linr }
function read-json { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSCustomObject] $input ) $json = [ordered]@{}; ($input).PSObject.Properties | % { $json[$_.Name] = $_.Value } $json.SyncRoot }
function read-paramNaliases ($command) { (Get-Command $command).parameters.values | select name, @{n='aliases';e={$_.aliases}} }
function read-pathsAsStream { get-childitem | out-string -stream } # filesInFolAsStream ;
function read-uptime { Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime'; EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}} } #doesn't psreadline module implement this already?
function Remove-CustomAliases { Get-Alias | Where-Object { ! $_.Options -match "ReadOnly" } | % { Remove-Item alias:$_ }} # https://stackoverflow.com/a/2816523
function remove-TempfilesNfolders { foreach ($folder in get-tempfilesNfolders) {Remove-Item $folder -force -recurse} }
function Sanatize-path { param( $path='G:\mhk',$replaceChar='_') if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $regex= "[^\)\w \\\ -:.åäöÅÄÖ %\ {,~$\(!@¤£é&=#§]" ; Get -ChildItem $path -Recurse | Where -Object {$_.name -match $regex} | rename -item -newName {$_.name -replace $regex,$replaceChar}}
function sed($path, $find, $replace) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } (Get-Content $path).replace("$find", $replace) | Set-Content $path }
function set-FileEncodingUtf8 ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } sc $path -encoding utf8 -value(gc $path) }
function set-x { Set-PSDebug -trace 2}
function set+x { Set-PSDebug -trace 0}
function sort-PathByLvl { param( $inputList) $inputList | Sort {($_ -split '\\').Count}, {$_} -Descending | select -object -first 2 | % { $error.clear() ; try { out -null -input (test -ModuleManifest $_ > '&2>&1' ) } catch { "Error" } ; if (!$error) { $_ } }}
function start-BrowserFlags { vivaldi "vivaldi://flags" } #todo: use standard browser instead of hardcoded
function start-notepad-profile { notepad $profile}
function string { process { $_ | Out-String -Stream } }
function touch($path) { "" | Out-File $path -Encoding ASCII }
function which($name) { Get-Command $name | Select-Object -ExpandProperty Definition } #should use more
Remove-Item alias:ls -ea SilentlyContinue ; function ls { Get-Childitem} # ls -al is musclememory by now so ignore all args for this "alias"
#------------------------------- Functions END -------------------------------
#------------------------------- Set alias BEGIN -------------------------------
$TAType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators") ; $TAType::Add('accelerators',$TAType)
set-alias accelerators [accelerators]::Get
set-alias edprofile start-Notepad-Profile
set-alias exp-pro open-ProfileFolder
set-alias kidStream get-childitem | out-string -stream
set-alias history (Get-PSReadlineOption).HistorySavePath
set-alias parameters get-parameters
set-alias start-su start-powershellAsAdmin
set-alias version $PSVersionTable # bash-like
Set-Alias cat Get-Content -Option AllScope
Set-Alias cd Set-Location -Option AllScope
Set-Alias clear Clear-Host -Option AllScope
Set-Alias cp Copy-Item -Option AllScope
Set-Alias history Get-History -Option AllScope
Set-Alias kill Stop-Process -Option AllScope
Set-Alias lp Out-Printer -Option AllScope
Set-Alias mv Move-Item -Option AllScope
Set-Alias ps Get-Process -Option AllScope
Set-Alias pwd Get-Location -Option AllScope
Set-Alias which Get-Command -Option AllScope
Set-Alias open Invoke-Item -Option AllScope
Set-Alias basename Split-Path -Option AllScope
Set-Alias realpath Resolve-Path -Option AllScope # cmd-like Set-Alias rm Remove-Item -Option AllScope
Set-Alias rmdir Remove-Item -Option AllScope
Set-Alias echo Write-Output -Option AllScope
Set-Alias cls Clear-Host -Option AllScope
Set-Alias chdir Set-Location -Option AllScope
Set-Alias copy Copy-Item -Option AllScope
Set-Alias del Remove-Item -Option AllScope
Set-Alias dir Get-Childitem -Option AllScope
Set-Alias erase Remove-Item -Option AllScope
Set-Alias move Move-Item -Option AllScope
Set-Alias rd Remove-Item -Option AllScope
Set-Alias ren Rename-Item -Option AllScope
Set-Alias set Set-Variable -Option AllScope
Set-Alias type Get-Content -Option AllScope
Set-Alias env Get-Environment -Option AllScope # custom aliases
Set-Alias flush-dns Clear-DnsClientCache -Option AllScope
Set-Alias touch Set-FileTime -Option AllScope
set-alias lsx get-Childnames -Option AllScope
set-alias filesinfolasstream read-childrenAsStream -Option AllScope
set-alias bcompare start-bc -Option AllScope
set-alias GitAdEPathAsSNB invoke-GitSubmoduleAdd -Option AllScope
set-alias GitUp invoke-GitLazy -Option AllScope
set-alias gitSilently invoke-GitLazySilently -Option AllScope
set-alias gitSingleRemote invoke-gitFetchOrig -Option AllScope
set-alias executeThis invoke-FuzzyWithEverything -Option AllScope
set-alias filesinfolasstream read-pathsAsStream -Option AllScope
set-alias everything invoke-Everything -Option AllScope
set-alias make invoke-Nmake -Option AllScope
set-alias MyAliases read-aliases -Option AllScope
set-alias OpenAsADmin invoke-powershellAsAdmin -Option AllScope
set-alias home open-here -Option AllScope
set-alias pastDo find-historyInvoke -Option AllScope
set-alias pastDoEdit find-historyAppendClipboard -Option AllScope
set-alias HistoryPath (Get-PSReadlineOption).HistorySavePath -Option AllScope
set-alias reboot exit-Nrenter -Option AllScope
set-alias browserflags start-BrowserFlags -Option AllScope
set-alias df get-volume -Option AllScope
set-alias printpaths read-EnvPaths -Option AllScope
set-alias reload initialize-profile -Option AllScope
set-alias uptime read-uptime -Option AllScope
set-alias getnic get-mac -Option AllScope # 1. 获取所有 Network Interface set-alias ll Get-ChildItem -Option AllScope
set-alias getip Get-IPv4Routes -Option AllScope
set-alias getip6 Get-IPv6Routes -Option AllScope
set-alias os-update Update-Packages -Option AllScope
set-alias remote invoke-gitRemote -Option AllScope
set-alias gitsplit subtree-split-rm-commit -Option AllScope
set-alias new-name Rename-Item -Option AllScope
set-alias set-name Rename-Item -Option AllScope
set-alias isFolder get-isFolder -Option AllScope
set-alias start-powershellAsAdmin invoke-powershellAsAdmin -Option AllScope
set-alias psVersion $PSVersionTable.PSVersion.Major -Option AllScope
#------------------------------- Set alias END -------------------------------
Write-Host "PSVersion: $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
Write-Host "PSEdition: $($PSVersionTable.PSEdition)"
Write-Host "Profile: $PSCommandPath"
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# src: https://gist.github.com/apfelchips/62a71500a0f044477698da71634ab87b
# New-Item $(Split-Path "$($PROFILE.CurrentUserCurrentHost)") -ItemType Directory -ea 0; Invoke-WebRequest -Uri "https://git.io/JYZTu" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
# ref: https://devblogs.microsoft.com/powershell/optimizing-your-profile/#measure-script
# ref: Powershell $? https://stackoverflow.com/a/55362991
# ref: Write-* https://stackoverflow.com/a/38527767
# Write-Host wrapper for Write-Information -InformationAction Continue
# define these environment variables if not set already and also provide them as PSVariables
if ( ( $null -eq $PSVersionTable.PSEdition) -or ($PSVersionTable.PSEdition -eq "Desktop") ) { $PSVersionTable.PSEdition = "Desktop" ;$IsWindows = $true }
if ( -not $IsWindows ) { function Test-IsAdmin { if ( (id -u) -eq 0 ) { return $true } return $false } }
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Clean-Object {
process {
$_.PSObject.Properties.Remove('PSComputerName')
$_.PSObject.Properties.Remove('RunspaceId')
$_.PSObject.Properties.Remove('PSShowComputerName')
}
#Where-Object { $_.PSObject.Properties.Value -ne $null}
}
function Get-Environment { # Get-Variable to show all Powershell Variables accessible via $
if ( $args.Count -eq 0 ) {
Get-Childitem env:
} elseif( $args.Count -eq 1 ) {
Start-Process (Get-Command $args[0]).Source
} else {
Start-Process (Get-Command $args[0]).Source -ArgumentList $args[1..($args.Count-1)]
}
}
function cf {
if ( $null -ne $(Get-Module PSFzf) ) {
Get-ChildItem . -Recurse -Attributes Directory | Invoke-Fzf | Set-Location
} else {
Write-Error "please install PSFzf"
}
}
if ( $(Test-CommandExists 'git') ) {
Set-Alias g git -Option AllScope
function git-root { $gitrootdir = (git rev-parse --show-toplevel) ; if ( $gitrootdir ) { Set-Location $gitrootdir } }
if ( $IsWindows ) {
function git-sh {
if ( $args.Count -eq 0 ) { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") -l
} else { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") $args }
}
function git-bash {
if ( $args.Count -eq 0 ) {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l
} else {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") $args
}
}
function git-vim { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l -c `'vim $args`' }
if ( -Not (Test-CommandExists 'sh') ){ Set-Alias sh git-sh -Option AllScope }
if ( -Not (Test-CommandExists 'bash') ){ Set-Alias bash git-bash -Option AllScope }
if ( -Not (Test-CommandExists 'vi') ){ Set-Alias vi git-vim -Option AllScope }
if ( -Not (Test-CommandExists 'vim') ){ Set-Alias vim git-vim -Option AllScope }
}
}
function Select-Value { # src: https://geekeefy.wordpress.com/2017/06/26/selecting-objects-by-value-in-powershell/
[Cmdletbinding()]
param(
[parameter(Mandatory=$true)] [String] $Value,
[parameter(ValueFromPipeline=$true)] $InputObject
)
process {
# Identify the PropertyName for respective matching Value, in order to populate it Default Properties
$Property = ($PSItem.properties.Where({$_.Value -Like "$Value"})).Name
If ( $Property ) {
# Create Property a set which includes the 'DefaultPropertySet' and Property for the respective 'Value' matched
$DefaultPropertySet = $PSItem.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
$TypeName = ($PSItem.PSTypenames)[0]
Get-TypeData $TypeName | Remove-TypeData
Update-TypeData -TypeName $TypeName -DefaultDisplayPropertySet ($DefaultPropertySet+$Property |Select-Object -Unique)
$PSItem | Where-Object {$_.properties.Value -like "$Value"}
}
}
}
function pause($message="Press any key to continue . . . ") {
Write-Host -NoNewline $message
$i=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183
while ($null -eq $k.VirtualKeyCode -or $i -Contains $k.VirtualKeyCode){
$k = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Write-Host ""
}
# native touch implementation
# src: https://ss64.com/ps/syntax-touch.html
function Set-FileTime {
param(
[string[]]$paths,
[bool]$only_modification = $false,
[bool]$only_access = $false
)
begin {
function updateFileSystemInfo([System.IO.FileSystemInfo]$fsInfo) {
$datetime = Get-Date
if ( $only_access ) {
$fsInfo.LastAccessTime = $datetime
} elseif ( $only_modification ) {
$fsInfo.LastWriteTime = $datetime
} else {
$fsInfo.CreationTime = $datetime
$fsInfo.LastWriteTime = $datetime
$fsInfo.LastAccessTime = $datetime
}
}
function touchExistingFile($arg) {
if ( $arg -is [System.IO.FileSystemInfo] ) {
updateFileSystemInfo($arg)
} else {
$resolvedPaths = Resolve-Path $arg
foreach ($rpath in $resolvedPaths) {
if ( Test-Path -type Container $rpath ) {
$fsInfo = New-Object System.IO.DirectoryInfo($rpath)
} else {
$fsInfo = New-Object System.IO.FileInfo($rpath)
}
updateFileSystemInfo($fsInfo)
}
}
}
function touchNewFile([string]$path) {
#$null > $path
Set-Content -Path $path -value $null;
}
}
process {
if ( $_ ) {
if ( Test-Path $_ ) {
touchExistingFile($_)
} else {
touchNewFile($_)
}
}
}
end {
if ( $paths ) {
foreach ( $path in $paths ) {
if ( Test-Path $path ) {
touchExistingFile($path)
} else {
touchNewFile($path)
}
}
}
}
}
if ( $IsWindows ) {
function stree($directory = $pwd) {
$gitrootdir = (Invoke-Command{Set-Location $args[0]; git rev-parse --show-toplevel 2>&1;} -ArgumentList $directory)
if ( Test-Path -Path "$gitrootdir\.git" -PathType Container) {
$newestExe = Get-Item "${env:ProgramFiles(x86)}\Atlassian\SourceTree\SourceTree.exe" | select -Last 1
Write-Debug "Opening $gitrootdir with $newestExe"
Start-Process -filepath $newestExe -ArgumentList "-f `"$gitrootdir`" log"
} else {
Write-Error "git directory not found"
}
}
if ( "${env:ChocolateyInstall}" -eq "" ) {
function Install-Chocolatey {
if (Get-Command choco -ErrorAction SilentlyContinue) {
Write-Error "chocolatey already installed!"
} else {
Start-Process (Get-HostExecutable) -ArgumentList "-Command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1') -verb RunAs"
}
}
else {
function choco { Start-Process (Get-HostExecutable) -ArgumentList "-noProfile -noLogo -Command choco.exe ${args}; pause" -verb runAs }
}
}
}
function Get-HostExecutable {
if ( $PSVersionTable.PSEdition -eq "Core" ) {
$ConsoleHostExecutable = (get-command pwsh).Source
} else {
$ConsoleHostExecutable = (get-command powershell).Source
}
return $ConsoleHostExecutable
}
# don't override chocolatey sudo or unix sudo
if ( -not $(Test-CommandExists 'sudo') ) {
function sudo() {
if ( $args.Length -eq 0 ) {
Start-Process $(Get-HostExecutable) -verb "runAs"
} elseif ( $args.Length -eq 1 ) {
Start-Process $args[0] -verb "runAs"
} else {
Start-Process $args[0] -ArgumentList $args[1..$args.Length] -verb "runAs"
}
}
}
# Helper Functions
#######################################################
function uptimef {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function unzipf ($path) {
$dirname = (Get-Item $path).Basename
echo("Extracting", $path, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $path -OutputPath $dirname -ShowProgress
}
# already expanded to save time https://github.com/nvbn/thefuck/wiki/Shell-aliases#powershell
if ( $(Test-CommandExists 'thefuck') ) {
function fuck {
$PYTHONIOENCODING_BKP=$env:PYTHONIOENCODING
$env:PYTHONIOENCODING="utf-8"
$history = (Get-History -Count 1).CommandLine
if (-not [string]::IsNullOrWhiteSpace($history)) {
$fuck = $(thefuck $args $history)
if ( -not [string]::IsNullOrWhiteSpace($fuck) ) {
if ( $fuck.StartsWith("echo") ) { $fuck = $fuck.Substring(5) } else { iex "$fuck" }
}
}
[Console]::ResetColor()
$env:PYTHONIOENCODING=$PYTHONIOENCODING_BKP
}
Set-Alias f fuck -Option AllScope
}
# hacks for old powerhsell versions
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
function Get-ExitBoolean($command) { & $command | Out-Null; $?} ; Set-Alias geb Get-ExitBoolean # fixed: https://github.com/PowerShell/PowerShell/pull/9849
function Use-Default # $var = d $Value : "DefaultValue" eg. ternary # fixed: https://toastit.dev/2019/09/25/ternary-operator-powershell-7/
{
for ($i = 1; $i -lt $args.Count; $i++){
if ($args[$i] -eq ":"){
$coord = $i; break
}
}
if ($coord -eq 0) {
throw new System.Exception "No operator!"
}
if ($args[$coord - 1] -eq ""){
$toReturn = $args[$coord + 1]
} else {
$toReturn = $args[$coord -1]
}
return $toReturn
}
Set-Alias d Use-Default
}
if ( $IsWindows ) {
# src: http://serverfault.com/questions/95431
function Test-IsAdmin { $user = [Security.Principal.WindowsIdentity]::GetCurrent(); return $(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator); }
function Reopen-here { Get-Process explorer | Stop-Process Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Get-Process explorer | Stop-Process'" -verb "runAs"}
function Reset-Spooler { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Stop-Service -Name Spooler -Force; Get-Item ${env:SystemRoot}\System32\spool\PRINTERS\* | Remove-Item -Force -Recurse; Start-Service -Name Spooler'" -verb "runAs" }
function subl { Start-Process "${Env:ProgramFiles}\Sublime Text\subl.exe" -ArgumentList $args -WindowStyle Hidden } # hide subl shim script
function get-tempfilesNfolders { foreach ($folder in @('C:\Windows\Temp\*', 'C:\Documents and Settings\*\Local Settings\temp\*', 'C:\Users\*\Appdata\Local\Temp\*', 'C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*', 'C:\Windows\SoftwareDistribution\Download', 'C:\Windows\System32\FNTCACHE.DAT')) {$_} }
function Export-Regestrykey { param ( $reg = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\' ,$outFile = 'H:\hkcu-regbackup.txt' ) get-childitem -path $reg | out-file $outFile }
function start-bc ($REMOTE,$LOCAL,$BASE,$MERGED) { cmd /c "${Env:ProgramFiles}\BeondCompare4\BComp.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" } #todo: replace hardcode with global variable pointing to path
function start-cygwin { param ( $cygwinpath = "${Env:ProgramFiles}\cygwin64\" ) ."$cygwinpath\Cygwin.bat" }
function Compare-ImagesMetadata { param ( $exifToolPath = "${Env:ProgramFiles}\geosetter\tools\" ,$inputA = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front (2).jpg" ,$inputB = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front.jpg" ) ; $set1 = .\exiftool.exe -a -u -g1 $inputA ; $set2 = .\exiftool.exe -a -u -g1 $inputB ; Compare-Object $set1 $set2 | select -ExpandProperty inputobject }
function new-SymbolicLink { param ( $where = 'H:\mina grejer\Till Github' ,$from = 'H:\mina grejer\Project shelf\Till Github' ) New-Item -Path $where -ItemType SymbolicLink -Value $from }
}
if (Test-CommandExists 'search-Everything')
{
function invoke-Everything([string]$filter) { @(Search-Everything -filter $filter -global) }
function invoke-FuzzyWithEverything($searchstring) { menu @(everything "ext:exe $searchString") | %{& $_ } } #use whatpulse db first, then everything #todo: sort by rescent use #use everything to find executable for fast execution
function Every-execute ($inputx) { $filter = "ext:exe $inputx" ; $filter } #& (Every-Menu $filter)
function Every-AsHashMap { param( $filter = 'ext:psd1 \module') $q = @{} ; everything $filter | %{@{ name = (get -item $_).name ; time=(get -item $_).LastWriteTime ; path=(get -item $_) } } | sort -object -property time | %{ $q[$_.name] = $_.path } ; $q | select -property values}
function Every-execute { param( $filter = 'regex:".*\\data\\[^\\]*.ahk"',$navigate=$true) Every-Menu | % { if($navigate) {cd ($_ | split -path -parent)} ; . $_ } }
function Every-Load { param( $psFileFilter = 'convert-xlsx-to-csv.ps1') . ( everythnig $psFileFilter | select -first 1) } ; invoke-expression "ExcelToCsv -File 'D:\unsorted\fannyUtskick.xlsx'"
function Every-Explore { param( $filter = 'ext:exe lasso') ; Every-Menu $filter | % { $path = if(!( Test-Path $_ -PathType Container)) { $_ | split-path -leaf } else {$_} ; explorer $path } }
function Every-Menu { param( $filter) $a= @(everything $filter) ; if($a.count -eq 1) {$a} else {menu $a} }
}
if (Test-CommandExists 'git')
{ #todo: move to git aliases
function invoke-gitCheckout () { & git checkout $args }
function invoke-gitFetchOrig { git fetch origin }
Function invoke-GitLazy($path,$message) { cd $path ; git lazy $message } ;
Function invoke-GitLazySilently {Out-File -FilePath .\lazy.log -inputObject (invoke-GitLazy 'AutoCommit' 2>&1 )} ; #todo: parameterize #todo: rename to more descriptive #todo: breakout
function invoke-gitRemote { param ($subCommand = 'get-url',$name = "origin" ) git remote $subCommand $name }
Function invoke-GitSubmoduleAdd([string]$leaf,[string]$remote,[string]$branch) { git submodule add -f --name $leaf -- $remote $branch ; git commit -am $leaf+$remote+$branch } ; #todo: move to git aliases #Git Ad $leaf as submodule from $remote and branch $branch
}
if ( $null -ne $(Get-Module PSReadline -ea SilentlyContinue)) {
function find-historyAppendClipboard($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{ Set-Clipboard -Value $_ }} #search history of past expressions and adds to clipboard
function find-historyInvoke($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{Invoke-Expression $_ } } #search history of past expressions and invokes it, doesn't register the expression itself in history, but the pastDo expression.
}
function split-fileByLineNr { param( $pathName = '.\gron.csv',$OutputFilenamePattern = 'output_done_' , $LineLimit = 60) ;
$ext = $pathName | split-path -Extension
$inputx = Get-Content ;
$line = 0 ;
$i = 0 ;
$path = 0 ;
$start = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq $LineLimit -Or $line -eq $inputx.Length) {
$path++ ;
$pathname = "$OutputFilenamePattern$path$ext" ;
$inputx[$start..($line - 1)] | Out -File $pathname -Force ;
$start = $line ;
$i = 0 ;
Write-Host "$pathname" ;
} ;
$i++ ;
$line++
}
}
function split-fileByMatch($pathName , $regex) { #param( $pathName = 'C:\Users\crbk01\Documents\WindowsPowerShell\snipps\Modules\Todo SplitUp.psm1' , $regex = '(?<=function\s)[^\s\(]*') ;
$ext = ($pathName | split-path -Extension)
$parent = ($pathName | split-path -Parent)
$OriginalName = ($pathName | split-path -LeafBase)
$inputx = Get-Content $pathName; $line = 0 ; $i = 0 ; $start = @(select-string -path $pathName -pattern $regex ) | select linenumber ; $LineLimit = $start | select -Skip 1 ; $names = @() ; [regex]::matches($inputx,$regex).groups.value | %{$names+= $_ }
$occurence = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq ([int]$LineLimit[$occurence].linenumber -1) -Or $line -eq $inputx.Length)
{
$currentName = $names[$occurence];
$pathname = Join-Path -path $parent -childPath "$OriginalName-$currentName$ext" ;
$u = ([int]$start[$occurence].linenumber -1)
$inputx[$u..($line - 1)] > $pathname
$occurence++ ;
Write-Host "$u..($line - 1)$pathname" ;
};
$i++ ;
$line++
}
}
#function aliasCode { & $env:code }
function .. { Set-Location ".." }
function .... { Set-Location (Join-Path -Path ".." -ChildPath "..") }
function all { process { $_ | Select-Object * } } # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#piping-objects-to-functions
function cdc { Set-Location "$XDG_CONFIG_HOME" }
function cdcheat { Set-Location "$CHEATS_DIR" }
function cdd { Set-Location "$DESKTOP_DIR" }
function cddd { Set-Location "$DEVEL_DIR" }
function cddev { Set-Location "$DEVEL_DIR" }
function cdn { Set-Location "$NOTES_DIR" }
function cdports { Set-Location "$PORTS_DIR" }
function cdt { Set-Location "$TODO_DIR" }
function clear-Days_Back { param( $path = "C:\Support\SQLBac\" ,$Daysback = "0" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item }
function ConvertFrom-Bytes { param( [string]$bytes, [string]$savepath ) $dir = Split-Path $savepath if (!(Test-Path $dir)) { md $dir | Out-Null } [convert]::FromBase64String($bytes) | Set-Content $savepath -Encoding Byte }
function ConvertTo-Bytes ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } [convert]::ToBase64String((Get-Content $path -Encoding Byte)) }
function df { get-volume }
function enter-dir { param( $path = '%USERPROFILE%\Desktop\' ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Set-Location $Path }; #if no param, navigate to desktop # 5. 更改工作目录 # 输入要切换到的路径 # 用法示例:cd C:/ # 默认路径:D 盘的桌面
function exit-Nrenter { shutdown /r } #reboot
function export($name, $value) { set-item -force -path "env:$name" -value $value; }
function foldercontain($folder,$name) { $q = get-childitem $folder; return $q -contains $name }
function Get-AllNic { Get-NetAdapter | Sort-Object -Property MacAddress } # 1. 获取所有 Network Interface
function get-Childnames { param( $path = $pwd) (Get-ChildItem -path $path).Name ; Write-Host("") }; # 3. 查看目录 ls & ll
function Get-DefaultAliases { Get-Alias | Where-Object { $_.Options -match "ReadOnly" }}
function get-envVar { Get-Childitem -Path Env:*}
function get-EspHeader { param( $path = '.\FormiD.esp', $max = 13,$headerSize = 3 ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $q = ((( -join (gc $path | select -first $headerSize)) -replace "[^\w. ] {1,}","å").split("å") -match "^.*[.](esp |esm)$") ; $q | ? {($q | select -first $max) -NotContains $_}}
function get-historyPath { (Get-PSReadlineOption).HistorySavePath }
function Get-IPv4Routes { Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } # 2. 获取 IPv4 关键路由
function Get-IPv6Routes { Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } # 3. 获取 IPv6 关键路由
function get-isFolder {$PSBoundParameters -is [system.in.folder]}
function get-parameters { Get-Member -Parameter *}
function get-RegInstallpaths { Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | % { Get -ItemProperty $_.PsPath } | Select DisplayName,InstallLocation }
function get-whatpulse { param( $program,$path) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $query = "select rightstr(path,instr(reverse(path),'/') -1) exe,path from (select max(path) path,max(cast(replace(version,'.','') as integer)) version from applications group by case when online_app_id = 0 then name else online_app_id end)" ; $adapter = newSqliteConnection -source (Everything 'whatpulse.db')[0] -query $query ; $b=@($data.item('exe')) ; $a = @($data.item('path')) ; $i=0 ; while($i -lt $a.Length) {$res[$b[$i]]=$a[$i] ; $i++ } ; $res | where { $_.name -match $program -and $_.path -match $path}}
function grep { process { $_ | Select-String -Pattern $args } } # function grep($regex, $dir) { if ( $dir ) { ls $dir | select-string $regex return } $input | select-string $regex }
function grepv($regex) { $input | ? { !$_.Contains($regex) } }
function Initialize-Profile { . $PROFILE.CurrentUserCurrentHost} #function initialize-profile { & $profile } #reload-profile is an unapproved verb.
function invoke-Nmake { nmake.exe $args -nologo }; # 1. 编译函数 make
function invoke-powershellAsAdmin { Start-Process powershell -Verb runAs } #new ps OpenAsADmin
function join-ByRuncunfig { param( $prefix='[$]APPLICATION_CONFIG_DIR[$][/]'
,$refixReplace='C:\Users\crbk01\AppData\Roaming\JetBrains\DataGrip2021.1\',
$runconfig="TillMinaMedelanden.run.xml",$output='runConfig/Combined.sql') [xml]$xml=get -content $runConfig -Encoding UTF8 ; $xml.component.configuration.'script -file' | %
{$_.value -replace $prefix, ($prefixReplace -replace '\\','/')} | % {" - -:$_"
; get -content -path $_ -Encoding UTF8 ; "go" } >> $output }
function list { process { $_ | Format-List * } } # fl is there by default
function man { Get-Help $args[0] | out-host -paging }
function md { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function measure-ExtOccurenseRecursivly { param( $path = "D:\Project Shelf\MapBasic" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Get-ChildItem -Path $path -Recurse -File | group Extension -NoElement | sort Count -Descending | select -Property name }
function measure-words { param( $inputStream = (Get -ChildItem | % { Get -Content $_.name }), $regex="[^a-z0-9_-]") $hash = @{} ; $a="" ; $inputStream | % {$a+=$_} ; $a -split $regex | % { $_.tolower()} | % {$hash[$_]++} ; $sorted= % {$hash.GetEnumerator() | sort -object {[int]$_.value}} ; return $sorted} ; $sorted | where{$_.name -notmatch "^\d+$"} | where{$_.name.length -gt 4 }
function mkdir { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function My-Scripts { Get-Command -CommandType externalscript }
function open-here { param( $Path = $pwd ) Invoke-Item $Path }; # 4. 打开当前工作目录 # 输入要打开的路径 # 用法示例:open C:\ # 默认路径:当前工作文件夹
function open-ProfileFolder { explorer (split-path -path $profile -parent)}
function pgrep($name) { Get-Process $name }
function pkill($name) { Get-Process $name -ErrorAction SilentlyContinue | kill }
function pull () { & get pull $args }
function read-aliases { Get-Alias | Where-Object { $_.Options -notmatch "ReadOnly" }}
function read-EnvPaths { ($Env:Path).Split(";") }
function read-headOfFile { param( $linr = 10, $path ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } gc -Path $path -TotalCount $linr }
function read-json { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSCustomObject] $input ) $json = [ordered]@{}; ($input).PSObject.Properties | % { $json[$_.Name] = $_.Value } $json.SyncRoot }
function read-paramNaliases ($command) { (Get-Command $command).parameters.values | select name, @{n='aliases';e={$_.aliases}} }
function read-pathsAsStream { get-childitem | out-string -stream } # filesInFolAsStream ;
function read-uptime { Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime'; EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}} } #doesn't psreadline module implement this already?
function Remove-CustomAliases { Get-Alias | Where-Object { ! $_.Options -match "ReadOnly" } | % { Remove-Item alias:$_ }} # https://stackoverflow.com/a/2816523
function remove-TempfilesNfolders { foreach ($folder in get-tempfilesNfolders) {Remove-Item $folder -force -recurse} }
function Sanatize-path { param( $path='G:\mhk',$replaceChar='_') if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $regex= "[^\)\w \\\ -:.åäöÅÄÖ %\ {,~$\(!@¤£é&=#§]" ; Get -ChildItem $path -Recurse | Where -Object {$_.name -match $regex} | rename -item -newName {$_.name -replace $regex,$replaceChar}}
function sed($path, $find, $replace) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } (Get-Content $path).replace("$find", $replace) | Set-Content $path }
function set-FileEncodingUtf8 ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } sc $path -encoding utf8 -value(gc $path) }
function set-x { Set-PSDebug -trace 2}
function set+x { Set-PSDebug -trace 0}
function sort-PathByLvl { param( $inputList) $inputList | Sort {($_ -split '\\').Count}, {$_} -Descending | select -object -first 2 | % { $error.clear() ; try { out -null -input (test -ModuleManifest $_ > '&2>&1' ) } catch { "Error" } ; if (!$error) { $_ } }}
function start-BrowserFlags { vivaldi "vivaldi://flags" } #todo: use standard browser instead of hardcoded
function start-notepad-profile { notepad $profile}
function string { process { $_ | Out-String -Stream } }
function touch($path) { "" | Out-File $path -Encoding ASCII }
function which($name) { Get-Command $name | Select-Object -ExpandProperty Definition } #should use more
Remove-Item alias:ls -ea SilentlyContinue ; function ls { Get-Childitem} # ls -al is musclememory by now so ignore all args for this "alias"
#------------------------------- Functions END -------------------------------
#------------------------------- Set alias BEGIN -------------------------------
$TAType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators") ; $TAType::Add('accelerators',$TAType)
set-alias accelerators [accelerators]::Get
set-alias edprofile start-Notepad-Profile
set-alias exp-pro open-ProfileFolder
set-alias kidStream get-childitem | out-string -stream
set-alias history (Get-PSReadlineOption).HistorySavePath
set-alias parameters get-parameters
set-alias start-su start-powershellAsAdmin
set-alias version $PSVersionTable # bash-like
Set-Alias cat Get-Content -Option AllScope
Set-Alias cd Set-Location -Option AllScope
Set-Alias clear Clear-Host -Option AllScope
Set-Alias cp Copy-Item -Option AllScope
Set-Alias history Get-History -Option AllScope
Set-Alias kill Stop-Process -Option AllScope
Set-Alias lp Out-Printer -Option AllScope
Set-Alias mv Move-Item -Option AllScope
Set-Alias ps Get-Process -Option AllScope
Set-Alias pwd Get-Location -Option AllScope
Set-Alias which Get-Command -Option AllScope
Set-Alias open Invoke-Item -Option AllScope
Set-Alias basename Split-Path -Option AllScope
Set-Alias realpath Resolve-Path -Option AllScope # cmd-like Set-Alias rm Remove-Item -Option AllScope
Set-Alias rmdir Remove-Item -Option AllScope
Set-Alias echo Write-Output -Option AllScope
Set-Alias cls Clear-Host -Option AllScope
Set-Alias chdir Set-Location -Option AllScope
Set-Alias copy Copy-Item -Option AllScope
Set-Alias del Remove-Item -Option AllScope
Set-Alias dir Get-Childitem -Option AllScope
Set-Alias erase Remove-Item -Option AllScope
Set-Alias move Move-Item -Option AllScope
Set-Alias rd Remove-Item -Option AllScope
Set-Alias ren Rename-Item -Option AllScope
Set-Alias set Set-Variable -Option AllScope
Set-Alias type Get-Content -Option AllScope
Set-Alias env Get-Environment -Option AllScope # custom aliases
Set-Alias flush-dns Clear-DnsClientCache -Option AllScope
Set-Alias touch Set-FileTime -Option AllScope
set-alias lsx get-Childnames -Option AllScope
set-alias filesinfolasstream read-childrenAsStream -Option AllScope
set-alias bcompare start-bc -Option AllScope
set-alias GitAdEPathAsSNB invoke-GitSubmoduleAdd -Option AllScope
set-alias GitUp invoke-GitLazy -Option AllScope
set-alias gitSilently invoke-GitLazySilently -Option AllScope
set-alias gitSingleRemote invoke-gitFetchOrig -Option AllScope
set-alias executeThis invoke-FuzzyWithEverything -Option AllScope
set-alias filesinfolasstream read-pathsAsStream -Option AllScope
set-alias everything invoke-Everything -Option AllScope
set-alias make invoke-Nmake -Option AllScope
set-alias MyAliases read-aliases -Option AllScope
set-alias OpenAsADmin invoke-powershellAsAdmin -Option AllScope
set-alias home open-here -Option AllScope
set-alias pastDo find-historyInvoke -Option AllScope
set-alias pastDoEdit find-historyAppendClipboard -Option AllScope
set-alias HistoryPath (Get-PSReadlineOption).HistorySavePath -Option AllScope
set-alias reboot exit-Nrenter -Option AllScope
set-alias browserflags start-BrowserFlags -Option AllScope
set-alias df get-volume -Option AllScope
set-alias printpaths read-EnvPaths -Option AllScope
set-alias reload initialize-profile -Option AllScope
set-alias uptime read-uptime -Option AllScope
set-alias getnic get-mac -Option AllScope # 1. 获取所有 Network Interface set-alias ll Get-ChildItem -Option AllScope
set-alias getip Get-IPv4Routes -Option AllScope
set-alias getip6 Get-IPv6Routes -Option AllScope
set-alias os-update Update-Packages -Option AllScope
set-alias remote invoke-gitRemote -Option AllScope
set-alias gitsplit subtree-split-rm-commit -Option AllScope
set-alias new-name Rename-Item -Option AllScope
set-alias set-name Rename-Item -Option AllScope
set-alias isFolder get-isFolder -Option AllScope
set-alias start-powershellAsAdmin invoke-powershellAsAdmin -Option AllScope
set-alias psVersion $PSVersionTable.PSVersion.Major -Option AllScope
#------------------------------- Set alias END -------------------------------
Write-Host "PSVersion: $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
Write-Host "PSEdition: $($PSVersionTable.PSEdition)"
Write-Host "Profile: $PSCommandPath"
#------------------------------- Credit to : apfelchips -------------------------------
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
function Install-PowerShellGet { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Repository PSGallery -Force -AllowClobber -SkipPublisherCheck; pause" -verb "RunAs"}
}
function Test-ModuleExists {
#retuns module version if exsists else false
Param ($name)
$x = Get-Module -ListAvailable -Name $name
return $x ?? $false
}
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Get-ModulesAvailable {
if ( $args.Count -eq 0 ) {
Get-Module -ListAvailable
} else {
Get-Module -ListAvailable $args
}
}
function Get-ModulesLoaded {
if ( $args.Count -eq 0 ) {
Get-Module -All
} else {
Get-Module -All $args
}
}
function TryImport-Module {
param($name)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$errorPath = join-path -Path (split-path $profile -Parent) -ChildPath "$name.error.load.log"
try { Import-Module $name && echo "i $name"}
catch { "er.loading $name" ; $error > $errorPath }
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Tryinstall-Module {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$errorPath = join-path -Path (split-path $profile -Parent) -ChildPath "$name.error.install.log"
try {
if ( $args.Count -eq 1 ) {
Invoke-Expression "PowerShellGet\Install-Module -Name $args[1] -Scope CurrentUser -Force -AllowClobber"
}
elseif ( $args.Count -eq 2 ) {
Invoke-Expression "PowerShellGet\Install-Module -Name $args[1] -Scope CurrentUser -Force -AllowClobber $args[2]"
}
elseif ($args.count -ne 0)
{
Invoke-Expression "PowerShellGet\Install-Module $args"
}
echo "i $name"
}
catch { "er.installing $name" ; $error > $errorPath }
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Install-MyModules {
Tryinstall-Module 'PSReadLine' -AllowPrerelease
Tryinstall-Module 'posh-git'
Tryinstall-Module 'PSFzf'
Tryinstall-Module 'PSEverything'
Tryinstall-Module 'PSProfiler' # --> Measure-Script
# serialization tools: eg. ConvertTo-HashString / ConvertTo-HashTable https://github.com/torgro/HashData
Tryinstall-Module 'hashdata'
# useful Tools eg. ConvertTo-FlatObject, Join-Object... https://github.com/RamblingCookieMonster/PowerShell
Tryinstall-Module 'WFTools'
# https://old.reddit.com/r/AZURE/comments/fh0ycv/azuread_vs_azurerm_vs_az/
# https://docs.microsoft.com/en-us/microsoft-365/enterprise/connect-to-microsoft-365-powershell
Tryinstall-Module 'AzureAD'
Tryinstall-Module 'Pscx'
Tryinstall-Module 'SqlServer'
if ( $IsWindows ){
# Windows Update CLI tool http://woshub.com/pswindowsupdate-module/#h2_2
# Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
# native alternative: WindowsUpdateProvider\Install-WUUpdates >= Windows Server 2019
Tryinstall-Module 'PSWindowsUpdate'
}
}
Import-Module -Name (join-path -Path (split-path $profile -Parent) -ChildPath "sqlite.ps1")
function Import-MyModules {
if (!( ""-eq "${env:ChocolateyInstall}" )) {
TryImport-Module "${env:ChocolateyInstall}\helpers\chocolateyProfile.psm1"
}
# does not load but test if avialable to speed up load time
# ForEach-Object { TryImport-Module -name $_ } #-parralel for ps 7 does not work currently
$modules = @( 'PowerShellGet', 'PSProfiler', 'hashdata','WFTools','AzureAD','SqlServer','PSWindowsUpdate','echoargs','pscx' )
$modules | ForEach-Object { $null = Test-ModuleExists $_ || "error $_" }
# 引入 posh-git
if ( ($host.Name -eq 'ConsoleHost') -and ($null -ne (Get-Module -ListAvailable -Name posh-git)) )
{ TryImport-Module posh-git }
# 引入 oh-my-posh
TryImport-Module oh-my-posh
if ( (Test-ModuleExists 'oh-my-posh' )) {
Set-PoshPrompt ys
Set-PoshPrompt paradox
}
# 设置 PowerShell 主题
# 引入 ps-read-line # useful history related actions
# example: https://github.com/PowerShell/PSReadLine/blob/master/PSReadLine/SamplePSReadLineProfile.ps1
if ( ($host.Name -eq 'ConsoleHost') -and (Test-ModuleExists 'PSReadLine' )) {
#TryImport-Module PSReadLine
#------------------------------- Set Hot-keys BEGIN -------------------------------
$PSReadLineOptions = @{
PredictionSource = "HistoryAndPlugin"
HistorySearchCursorMovesToEnd = $true
}
Set-PSReadLineOption @PSReadLineOptions
# Set-PSReadLineOption -EditMode Emac
# 每次回溯输入历史,光标定位于输入内容末尾
# 设置 Tab 为菜单补全和 Intellisense
# 设置 Ctrl+d 为退出 PowerShell
# 设置 Ctrl+z 为撤销
# 设置向上键为后向搜索历史记录 # Autocompletion for arrow keys @ https://dev.to/ofhouse/add-a-bash-like-autocomplete-to-your-powershell-4257
Set-PSReadlineKeyHandler -Chord 'Shift+Tab' -Function Complete
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#------------------------------- Set Hot-keys END -------------------------------
if ( $(Get-Module PSReadline).Version -ge 2.2 ) {
# 设置预测文本来源为历史记录
Set-PSReadLineOption -predictionsource history -ea SilentlyContinue
}
if ( $null -ne $(Get-Module PSFzf) ) {
#Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }
#$FZF_COMPLETION_TRIGGER='...'
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
}
}
}
Import-MyModules; echo "modules imported"
The MIT License (MIT)
Copyright (c) 2022 Christoffer Brobäck
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#------------------------------- Credit to : apfelchips -------------------------------
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
function Install-PowerShellGet { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Repository PSGallery -Force -AllowClobber -SkipPublisherCheck; pause" -verb "RunAs"}
}
function Test-ModuleExists {
#retuns module version if exsists else false
Param ($name)
$x = Get-Module -ListAvailable -Name $name
return $x ?? $false
}
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Get-ModulesAvailable {
if ( $args.Count -eq 0 ) {
Get-Module -ListAvailable
} else {
Get-Module -ListAvailable $args
}
}
function Get-ModulesLoaded {
if ( $args.Count -eq 0 ) {
Get-Module -All
} else {
Get-Module -All $args
}
}
function TryImport-Module {
param($name)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$errorPath = join-path -Path (split-path $profile -Parent) -ChildPath "$name.error.load.log"
try { Import-Module $name && echo "i $name"}
catch { "er.loading $name" ; $error > $errorPath }
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Tryinstall-Module {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$errorPath = join-path -Path (split-path $profile -Parent) -ChildPath "$name.error.install.log"
try {
if ( $args.Count -eq 1 ) {
Invoke-Expression "PowerShellGet\Install-Module -Name $args[1] -Scope CurrentUser -Force -AllowClobber"
}
elseif ( $args.Count -eq 2 ) {
Invoke-Expression "PowerShellGet\Install-Module -Name $args[1] -Scope CurrentUser -Force -AllowClobber $args[2]"
}
elseif ($args.count -ne 0)
{
Invoke-Expression "PowerShellGet\Install-Module $args"
}
echo "i $name"
}
catch { "er.installing $name" ; $error > $errorPath }
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Install-MyModules {
Tryinstall-Module 'PSReadLine' -AllowPrerelease
Tryinstall-Module 'posh-git'
Tryinstall-Module 'PSFzf'
Tryinstall-Module 'PSEverything'
Tryinstall-Module 'PSProfiler' # --> Measure-Script
# serialization tools: eg. ConvertTo-HashString / ConvertTo-HashTable https://github.com/torgro/HashData
Tryinstall-Module 'hashdata'
# useful Tools eg. ConvertTo-FlatObject, Join-Object... https://github.com/RamblingCookieMonster/PowerShell
Tryinstall-Module 'WFTools'
# https://old.reddit.com/r/AZURE/comments/fh0ycv/azuread_vs_azurerm_vs_az/
# https://docs.microsoft.com/en-us/microsoft-365/enterprise/connect-to-microsoft-365-powershell
Tryinstall-Module 'AzureAD'
Tryinstall-Module 'Pscx'
Tryinstall-Module 'SqlServer'
if ( $IsWindows ){
# Windows Update CLI tool http://woshub.com/pswindowsupdate-module/#h2_2
# Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
# native alternative: WindowsUpdateProvider\Install-WUUpdates >= Windows Server 2019
Tryinstall-Module 'PSWindowsUpdate'
}
}
Import-Module -Name (join-path -Path (split-path $profile -Parent) -ChildPath "sqlite.ps1")
function Import-MyModules {
if (!( ""-eq "${env:ChocolateyInstall}" )) {
TryImport-Module "${env:ChocolateyInstall}\helpers\chocolateyProfile.psm1"
}
# does not load but test if avialable to speed up load time
# ForEach-Object { TryImport-Module -name $_ } #-parralel for ps 7 does not work currently
$modules = @( 'PowerShellGet', 'PSProfiler', 'hashdata','WFTools','AzureAD','SqlServer','PSWindowsUpdate','echoargs','pscx' )
$modules | ForEach-Object { $null = Test-ModuleExists $_ || "error $_" }
# 引入 posh-git
if ( ($host.Name -eq 'ConsoleHost') -and ($null -ne (Get-Module -ListAvailable -Name posh-git)) )
{ TryImport-Module posh-git }
# 引入 oh-my-posh
TryImport-Module oh-my-posh
if ( (Test-ModuleExists 'oh-my-posh' )) {
Set-PoshPrompt ys
Set-PoshPrompt paradox
}
# 设置 PowerShell 主题
# 引入 ps-read-line # useful history related actions
# example: https://github.com/PowerShell/PSReadLine/blob/master/PSReadLine/SamplePSReadLineProfile.ps1
if ( ($host.Name -eq 'ConsoleHost') -and (Test-ModuleExists 'PSReadLine' )) {
#TryImport-Module PSReadLine
#------------------------------- Set Hot-keys BEGIN -------------------------------
$PSReadLineOptions = @{
PredictionSource = "HistoryAndPlugin"
HistorySearchCursorMovesToEnd = $true
}
Set-PSReadLineOption @PSReadLineOptions
# Set-PSReadLineOption -EditMode Emac
# 每次回溯输入历史,光标定位于输入内容末尾
# 设置 Tab 为菜单补全和 Intellisense
# 设置 Ctrl+d 为退出 PowerShell
# 设置 Ctrl+z 为撤销
# 设置向上键为后向搜索历史记录 # Autocompletion for arrow keys @ https://dev.to/ofhouse/add-a-bash-like-autocomplete-to-your-powershell-4257
Set-PSReadlineKeyHandler -Chord 'Shift+Tab' -Function Complete
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#------------------------------- Set Hot-keys END -------------------------------
if ( $(Get-Module PSReadline).Version -ge 2.2 ) {
# 设置预测文本来源为历史记录
Set-PSReadLineOption -predictionsource history -ea SilentlyContinue
}
if ( $null -ne $(Get-Module PSFzf) ) {
#Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }
#$FZF_COMPLETION_TRIGGER='...'
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
}
}
}
Import-MyModules; echo "modules imported"
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# src: https://gist.github.com/apfelchips/62a71500a0f044477698da71634ab87b
# New-Item $(Split-Path "$($PROFILE.CurrentUserCurrentHost)") -ItemType Directory -ea 0; Invoke-WebRequest -Uri "https://git.io/JYZTu" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
# ref: https://devblogs.microsoft.com/powershell/optimizing-your-profile/#measure-script
# ref: Powershell $? https://stackoverflow.com/a/55362991
# ref: Write-* https://stackoverflow.com/a/38527767
# Write-Host wrapper for Write-Information -InformationAction Continue
# define these environment variables if not set already and also provide them as PSVariables
if ( ( $null -eq $PSVersionTable.PSEdition) -or ($PSVersionTable.PSEdition -eq "Desktop") ) { $PSVersionTable.PSEdition = "Desktop" ;$IsWindows = $true }
if ( -not $IsWindows ) { function Test-IsAdmin { if ( (id -u) -eq 0 ) { return $true } return $false } }
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Clean-Object {
process {
$_.PSObject.Properties.Remove('PSComputerName')
$_.PSObject.Properties.Remove('RunspaceId')
$_.PSObject.Properties.Remove('PSShowComputerName')
}
#Where-Object { $_.PSObject.Properties.Value -ne $null}
}
function Get-Environment { # Get-Variable to show all Powershell Variables accessible via $
if ( $args.Count -eq 0 ) {
Get-Childitem env:
} elseif( $args.Count -eq 1 ) {
Start-Process (Get-Command $args[0]).Source
} else {
Start-Process (Get-Command $args[0]).Source -ArgumentList $args[1..($args.Count-1)]
}
}
function cf {
if ( $null -ne $(Get-Module PSFzf) ) {
Get-ChildItem . -Recurse -Attributes Directory | Invoke-Fzf | Set-Location
} else {
Write-Error "please install PSFzf"
}
}
if ( $(Test-CommandExists 'git') ) {
Set-Alias g git -Option AllScope
function git-root { $gitrootdir = (git rev-parse --show-toplevel) ; if ( $gitrootdir ) { Set-Location $gitrootdir } }
if ( $IsWindows ) {
function git-sh {
if ( $args.Count -eq 0 ) { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") -l
} else { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") $args }
}
function git-bash {
if ( $args.Count -eq 0 ) {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l
} else {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") $args
}
}
function git-vim { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l -c `'vim $args`' }
if ( -Not (Test-CommandExists 'sh') ){ Set-Alias sh git-sh -Option AllScope }
if ( -Not (Test-CommandExists 'bash') ){ Set-Alias bash git-bash -Option AllScope }
if ( -Not (Test-CommandExists 'vi') ){ Set-Alias vi git-vim -Option AllScope }
if ( -Not (Test-CommandExists 'vim') ){ Set-Alias vim git-vim -Option AllScope }
}
}
function Select-Value { # src: https://geekeefy.wordpress.com/2017/06/26/selecting-objects-by-value-in-powershell/
[Cmdletbinding()]
param(
[parameter(Mandatory=$true)] [String] $Value,
[parameter(ValueFromPipeline=$true)] $InputObject
)
process {
# Identify the PropertyName for respective matching Value, in order to populate it Default Properties
$Property = ($PSItem.properties.Where({$_.Value -Like "$Value"})).Name
If ( $Property ) {
# Create Property a set which includes the 'DefaultPropertySet' and Property for the respective 'Value' matched
$DefaultPropertySet = $PSItem.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
$TypeName = ($PSItem.PSTypenames)[0]
Get-TypeData $TypeName | Remove-TypeData
Update-TypeData -TypeName $TypeName -DefaultDisplayPropertySet ($DefaultPropertySet+$Property |Select-Object -Unique)
$PSItem | Where-Object {$_.properties.Value -like "$Value"}
}
}
}
function pause($message="Press any key to continue . . . ") {
Write-Host -NoNewline $message
$i=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183
while ($null -eq $k.VirtualKeyCode -or $i -Contains $k.VirtualKeyCode){
$k = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Write-Host ""
}
# native touch implementation
# src: https://ss64.com/ps/syntax-touch.html
function Set-FileTime {
param(
[string[]]$paths,
[bool]$only_modification = $false,
[bool]$only_access = $false
)
begin {
function updateFileSystemInfo([System.IO.FileSystemInfo]$fsInfo) {
$datetime = Get-Date
if ( $only_access ) {
$fsInfo.LastAccessTime = $datetime
} elseif ( $only_modification ) {
$fsInfo.LastWriteTime = $datetime
} else {
$fsInfo.CreationTime = $datetime
$fsInfo.LastWriteTime = $datetime
$fsInfo.LastAccessTime = $datetime
}
}
function touchExistingFile($arg) {
if ( $arg -is [System.IO.FileSystemInfo] ) {
updateFileSystemInfo($arg)
} else {
$resolvedPaths = Resolve-Path $arg
foreach ($rpath in $resolvedPaths) {
if ( Test-Path -type Container $rpath ) {
$fsInfo = New-Object System.IO.DirectoryInfo($rpath)
} else {
$fsInfo = New-Object System.IO.FileInfo($rpath)
}
updateFileSystemInfo($fsInfo)
}
}
}
function touchNewFile([string]$path) {
#$null > $path
Set-Content -Path $path -value $null;
}
}
process {
if ( $_ ) {
if ( Test-Path $_ ) {
touchExistingFile($_)
} else {
touchNewFile($_)
}
}
}
end {
if ( $paths ) {
foreach ( $path in $paths ) {
if ( Test-Path $path ) {
touchExistingFile($path)
} else {
touchNewFile($path)
}
}
}
}
}
if ( $IsWindows ) {
function stree($directory = $pwd) {
$gitrootdir = (Invoke-Command{Set-Location $args[0]; git rev-parse --show-toplevel 2>&1;} -ArgumentList $directory)
if ( Test-Path -Path "$gitrootdir\.git" -PathType Container) {
$newestExe = Get-Item "${env:ProgramFiles(x86)}\Atlassian\SourceTree\SourceTree.exe" | select -Last 1
Write-Debug "Opening $gitrootdir with $newestExe"
Start-Process -filepath $newestExe -ArgumentList "-f `"$gitrootdir`" log"
} else {
Write-Error "git directory not found"
}
}
if ( "${env:ChocolateyInstall}" -eq "" ) {
function Install-Chocolatey {
if (Get-Command choco -ErrorAction SilentlyContinue) {
Write-Error "chocolatey already installed!"
} else {
Start-Process (Get-HostExecutable) -ArgumentList "-Command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1') -verb RunAs"
}
}
else {
function choco { Start-Process (Get-HostExecutable) -ArgumentList "-noProfile -noLogo -Command choco.exe ${args}; pause" -verb runAs }
}
}
}
function Get-HostExecutable {
if ( $PSVersionTable.PSEdition -eq "Core" ) {
$ConsoleHostExecutable = (get-command pwsh).Source
} else {
$ConsoleHostExecutable = (get-command powershell).Source
}
return $ConsoleHostExecutable
}
# don't override chocolatey sudo or unix sudo
if ( -not $(Test-CommandExists 'sudo') ) {
function sudo() {
if ( $args.Length -eq 0 ) {
Start-Process $(Get-HostExecutable) -verb "runAs"
} elseif ( $args.Length -eq 1 ) {
Start-Process $args[0] -verb "runAs"
} else {
Start-Process $args[0] -ArgumentList $args[1..$args.Length] -verb "runAs"
}
}
}
# Helper Functions
#######################################################
function uptimef {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function unzipf ($path) {
$dirname = (Get-Item $path).Basename
echo("Extracting", $path, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $path -OutputPath $dirname -ShowProgress
}
# already expanded to save time https://github.com/nvbn/thefuck/wiki/Shell-aliases#powershell
if ( $(Test-CommandExists 'thefuck') ) {
function fuck {
$PYTHONIOENCODING_BKP=$env:PYTHONIOENCODING
$env:PYTHONIOENCODING="utf-8"
$history = (Get-History -Count 1).CommandLine
if (-not [string]::IsNullOrWhiteSpace($history)) {
$fuck = $(thefuck $args $history)
if ( -not [string]::IsNullOrWhiteSpace($fuck) ) {
if ( $fuck.StartsWith("echo") ) { $fuck = $fuck.Substring(5) } else { iex "$fuck" }
}
}
[Console]::ResetColor()
$env:PYTHONIOENCODING=$PYTHONIOENCODING_BKP
}
Set-Alias f fuck -Option AllScope
}
# hacks for old powerhsell versions
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
function Get-ExitBoolean($command) { & $command | Out-Null; $?} ; Set-Alias geb Get-ExitBoolean # fixed: https://github.com/PowerShell/PowerShell/pull/9849
function Use-Default # $var = d $Value : "DefaultValue" eg. ternary # fixed: https://toastit.dev/2019/09/25/ternary-operator-powershell-7/
{
for ($i = 1; $i -lt $args.Count; $i++){
if ($args[$i] -eq ":"){
$coord = $i; break
}
}
if ($coord -eq 0) {
throw new System.Exception "No operator!"
}
if ($args[$coord - 1] -eq ""){
$toReturn = $args[$coord + 1]
} else {
$toReturn = $args[$coord -1]
}
return $toReturn
}
Set-Alias d Use-Default
}
if ( $IsWindows ) {
# src: http://serverfault.com/questions/95431
function Test-IsAdmin { $user = [Security.Principal.WindowsIdentity]::GetCurrent(); return $(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator); }
function Reopen-here { Get-Process explorer | Stop-Process Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Get-Process explorer | Stop-Process'" -verb "runAs"}
function Reset-Spooler { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Stop-Service -Name Spooler -Force; Get-Item ${env:SystemRoot}\System32\spool\PRINTERS\* | Remove-Item -Force -Recurse; Start-Service -Name Spooler'" -verb "runAs" }
function subl { Start-Process "${Env:ProgramFiles}\Sublime Text\subl.exe" -ArgumentList $args -WindowStyle Hidden } # hide subl shim script
function get-tempfilesNfolders { foreach ($folder in @('C:\Windows\Temp\*', 'C:\Documents and Settings\*\Local Settings\temp\*', 'C:\Users\*\Appdata\Local\Temp\*', 'C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*', 'C:\Windows\SoftwareDistribution\Download', 'C:\Windows\System32\FNTCACHE.DAT')) {$_} }
function Export-Regestrykey { param ( $reg = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\' ,$outFile = 'H:\hkcu-regbackup.txt' ) get-childitem -path $reg | out-file $outFile }
function start-bc ($REMOTE,$LOCAL,$BASE,$MERGED) { cmd /c "${Env:ProgramFiles}\BeondCompare4\BComp.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" } #todo: replace hardcode with global variable pointing to path
function start-cygwin { param ( $cygwinpath = "${Env:ProgramFiles}\cygwin64\" ) ."$cygwinpath\Cygwin.bat" }
function Compare-ImagesMetadata { param ( $exifToolPath = "${Env:ProgramFiles}\geosetter\tools\" ,$inputA = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front (2).jpg" ,$inputB = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front.jpg" ) ; $set1 = .\exiftool.exe -a -u -g1 $inputA ; $set2 = .\exiftool.exe -a -u -g1 $inputB ; Compare-Object $set1 $set2 | select -ExpandProperty inputobject }
function new-SymbolicLink { param ( $where = 'H:\mina grejer\Till Github' ,$from = 'H:\mina grejer\Project shelf\Till Github' ) New-Item -Path $where -ItemType SymbolicLink -Value $from }
}
if (Test-CommandExists 'search-Everything')
{
function invoke-Everything([string]$filter) { @(Search-Everything -filter $filter -global) }
function invoke-FuzzyWithEverything($searchstring) { menu @(everything "ext:exe $searchString") | %{& $_ } } #use whatpulse db first, then everything #todo: sort by rescent use #use everything to find executable for fast execution
function Every-execute ($inputx) { $filter = "ext:exe $inputx" ; $filter } #& (Every-Menu $filter)
function Every-AsHashMap { param( $filter = 'ext:psd1 \module') $q = @{} ; everything $filter | %{@{ name = (get -item $_).name ; time=(get -item $_).LastWriteTime ; path=(get -item $_) } } | sort -object -property time | %{ $q[$_.name] = $_.path } ; $q | select -property values}
function Every-execute { param( $filter = 'regex:".*\\data\\[^\\]*.ahk"',$navigate=$true) Every-Menu | % { if($navigate) {cd ($_ | split -path -parent)} ; . $_ } }
function Every-invoke-expression { param( $psFileFilter = 'convert-xlsx-to-csv.ps1') . ( everythnig $psFileFilter | select -first 1) } ; invoke-expression "ExcelToCsv -File 'D:\unsorted\fannyUtskick.xlsx'"
function Every-Explore { param( $filter = 'ext:exe lasso') ; Every-Menu $filter | % { $path = if(!( Test-Path $_ -PathType Container)) { $_ | split-path -leaf } else {$_} ; explorer $path } }
function Every-Menu { param( $filter) $a= @(everything $filter) ; if($a.count -eq 1) {$a} else {menu $a} }
function Every-AddType { param( $filter) $a = Every-Menu $filter ; Add-Type -Path $a }
}
if (Test-CommandExists 'git')
{ #todo: move to git aliases
function invoke-gitCheckout () { & git checkout $args }
function invoke-gitFetchOrig { git fetch origin }
Function invoke-GitLazy($path,$message) { cd $path ; git lazy $message } ;
Function invoke-GitLazySilently {Out-File -FilePath .\lazy.log -inputObject (invoke-GitLazy 'AutoCommit' 2>&1 )} ; #todo: parameterize #todo: rename to more descriptive #todo: breakout
function invoke-gitRemote { param ($subCommand = 'get-url',$name = "origin" ) git remote $subCommand $name }
Function invoke-GitSubmoduleAdd([string]$leaf,[string]$remote,[string]$branch) { git submodule add -f --name $leaf -- $remote $branch ; git commit -am $leaf+$remote+$branch } ; #todo: move to git aliases #Git Ad $leaf as submodule from $remote and branch $branch
function TortCommit ($path) { & 'C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe' /command:commit }
function TortCommit ($path,$message) { & 'C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe' /command:commit /path:$path /logmsg:$messsage }
}
if ( $null -ne $(Get-Module PSReadline -ea SilentlyContinue)) {
function find-historyAppendClipboard($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{ Set-Clipboard -Value $_ }} #search history of past expressions and adds to clipboard
function find-historyInvoke($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{Invoke-Expression $_ } } #search history of past expressions and invokes it, doesn't register the expression itself in history, but the pastDo expression.
}
function split-fileByLineNr { param( $pathName = '.\gron.csv',$OutputFilenamePattern = 'output_done_' , $LineLimit = 60) ;
$ext = $pathName | split-path -Extension
$inputx = Get-Content ;
$line = 0 ;
$i = 0 ;
$path = 0 ;
$start = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq $LineLimit -Or $line -eq $inputx.Length) {
$path++ ;
$pathname = "$OutputFilenamePattern$path$ext" ;
$inputx[$start..($line - 1)] | Out -File $pathname -Force ;
$start = $line ;
$i = 0 ;
Write-Host "$pathname" ;
} ;
$i++ ;
$line++
}
}
function split-fileByMatch($pathName , $regex) { #param( $pathName = 'C:\Users\crbk01\Documents\WindowsPowerShell\snipps\Modules\Todo SplitUp.psm1' , $regex = '(?<=function\s)[^\s\(]*') ;
$ext = ($pathName | split-path -Extension)
$parent = ($pathName | split-path -Parent)
$OriginalName = ($pathName | split-path -LeafBase)
$inputx = Get-Content $pathName; $line = 0 ; $i = 0 ; $start = @(select-string -path $pathName -pattern $regex ) | select linenumber ; $LineLimit = $start | select -Skip 1 ; $names = @() ; [regex]::matches($inputx,$regex).groups.value | %{$names+= $_ }
$occurence = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq ([int]$LineLimit[$occurence].linenumber -1) -Or $line -eq $inputx.Length)
{
$currentName = $names[$occurence];
$pathname = Join-Path -path $parent -childPath "$OriginalName-$currentName$ext" ;
$u = ([int]$start[$occurence].linenumber -1)
$inputx[$u..($line - 1)] > $pathname
$occurence++ ;
Write-Host "$u..($line - 1)$pathname" ;
};
$i++ ;
$line++
}
}
#function aliasCode { & $env:code }
function .. { Set-Location ".." }
function .... { Set-Location (Join-Path -Path ".." -ChildPath "..") }
function all { process { $_ | Select-Object * } } # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#piping-objects-to-functions
function cdc { Set-Location "$XDG_CONFIG_HOME" }
function cdcheat { Set-Location "$CHEATS_DIR" }
function cdd { Set-Location "$DESKTOP_DIR" }
function cddd { Set-Location "$DEVEL_DIR" }
function cddev { Set-Location "$DEVEL_DIR" }
function cdn { Set-Location "$NOTES_DIR" }
function cdports { Set-Location "$PORTS_DIR" }
function cdt { Set-Location "$TODO_DIR" }
function clear-Days_Back { param( $path = "C:\Support\SQLBac\" ,$Daysback = "0" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item }
function ConvertFrom-Bytes { param( [string]$bytes, [string]$savepath ) $dir = Split-Path $savepath if (!(Test-Path $dir)) { md $dir | Out-Null } [convert]::FromBase64String($bytes) | Set-Content $savepath -Encoding Byte }
function ConvertTo-Bytes ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } [convert]::ToBase64String((Get-Content $path -Encoding Byte)) }
function df { get-volume }
function enter-dir { param( $path = '%USERPROFILE%\Desktop\' ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Set-Location $Path }; #if no param, navigate to desktop # 5. 更改工作目录 # 输入要切换到的路径 # 用法示例:cd C:/ # 默认路径:D 盘的桌面
function exit-Nrenter { shutdown /r } #reboot
function export($name, $value) { set-item -force -path "env:$name" -value $value; }
function foldercontain($folder,$name) { $q = get-childitem $folder; return $q -contains $name }
function Get-AllNic { Get-NetAdapter | Sort-Object -Property MacAddress } # 1. 获取所有 Network Interface
function get-Childnames { param( $path = $pwd) (Get-ChildItem -path $path).Name ; Write-Host("") }; # 3. 查看目录 ls & ll
function Get-DefaultAliases { Get-Alias | Where-Object { $_.Options -match "ReadOnly" }}
function get-envVar { Get-Childitem -Path Env:*}
function get-EspHeader { param( $path = '.\FormiD.esp', $max = 13,$headerSize = 3 ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $q = ((( -join (gc $path | select -first $headerSize)) -replace "[^\w. ] {1,}","å").split("å") -match "^.*[.](esp |esm)$") ; $q | ? {($q | select -first $max) -NotContains $_}}
function get-historyPath { (Get-PSReadlineOption).HistorySavePath }
function Get-IPv4Routes { Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } # 2. 获取 IPv4 关键路由
function Get-IPv6Routes { Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } # 3. 获取 IPv6 关键路由
function get-isFolder {$PSBoundParameters -is [system.in.folder]}
function get-parameters { Get-Member -Parameter *}
function get-RegInstallpaths { Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | % { Get -ItemProperty $_.PsPath } | Select DisplayName,InstallLocation }
function get-whatpulse { param( $program,$path) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $query = "select rightstr(path,instr(reverse(path),'/') -1) exe,path from (select max(path) path,max(cast(replace(version,'.','') as integer)) version from applications group by case when online_app_id = 0 then name else online_app_id end)" ; $adapter = newSqliteConnection -source (Everything 'whatpulse.db')[0] -query $query ; $b=@($data.item('exe')) ; $a = @($data.item('path')) ; $i=0 ; while($i -lt $a.Length) {$res[$b[$i]]=$a[$i] ; $i++ } ; $res | where { $_.name -match $program -and $_.path -match $path}}
function grep { process { $_ | Select-String -Pattern $args } } # function grep($regex, $dir) { if ( $dir ) { ls $dir | select-string $regex return } $input | select-string $regex }
function grepv($regex) { $input | ? { !$_.Contains($regex) } }
function Initialize-Profile { . $PROFILE.CurrentUserCurrentHost} #function initialize-profile { & $profile } #reload-profile is an unapproved verb.
function invoke-Nmake { nmake.exe $args -nologo }; # 1. 编译函数 make
function invoke-powershellAsAdmin { Start-Process powershell -Verb runAs } #new ps OpenAsADmin
function join-ByRuncunfig { param( $prefix='[$]APPLICATION_CONFIG_DIR[$][/]'
,$refixReplace='C:\Users\crbk01\AppData\Roaming\JetBrains\DataGrip2021.1\',
$runconfig="TillMinaMedelanden.run.xml",$output='runConfig/Combined.sql') [xml]$xml=get -content $runConfig -Encoding UTF8 ; $xml.component.configuration.'script -file' | %
{$_.value -replace $prefix, ($prefixReplace -replace '\\','/')} | % {" - -:$_"
; get -content -path $_ -Encoding UTF8 ; "go" } >> $output }
function list { process { $_ | Format-List * } } # fl is there by default
function man { Get-Help $args[0] | out-host -paging }
function md { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function measure-ExtOccurenseRecursivly { param( $path = "D:\Project Shelf\MapBasic" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Get-ChildItem -Path $path -Recurse -File | group Extension -NoElement | sort Count -Descending | select -Property name }
function measure-words { param( $inputStream = (Get -ChildItem | % { Get -Content $_.name }), $regex="[^a-z0-9_-]") $hash = @{} ; $a="" ; $inputStream | % {$a+=$_} ; $a -split $regex | % { $_.tolower()} | % {$hash[$_]++} ; $sorted= % {$hash.GetEnumerator() | sort -object {[int]$_.value}} ; return $sorted} ; $sorted | where{$_.name -notmatch "^\d+$"} | where{$_.name.length -gt 4 }
function mkdir { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function My-Scripts { Get-Command -CommandType externalscript }
function open-here { param( $Path = $pwd ) Invoke-Item $Path }; # 4. 打开当前工作目录 # 输入要打开的路径 # 用法示例:open C:\ # 默认路径:当前工作文件夹
function open-ProfileFolder { explorer (split-path -path $profile -parent)}
function pgrep($name) { Get-Process $name }
function pkill($name) { Get-Process $name -ErrorAction SilentlyContinue | kill }
function pull () { & get pull $args }
function read-aliases { Get-Alias | Where-Object { $_.Options -notmatch "ReadOnly" }}
function read-EnvPaths { ($Env:Path).Split(";") }
function read-headOfFile { param( $linr = 10, $path ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } gc -Path $path -TotalCount $linr }
function read-json { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSCustomObject] $input ) $json = [ordered]@{}; ($input).PSObject.Properties | % { $json[$_.Name] = $_.Value } $json.SyncRoot }
function read-paramNaliases ($command) { (Get-Command $command).parameters.values | select name, @{n='aliases';e={$_.aliases}} }
function read-pathsAsStream { get-childitem | out-string -stream } # filesInFolAsStream ;
function read-uptime { Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime'; EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}} } #doesn't psreadline module implement this already?
function Remove-CustomAliases { Get-Alias | Where-Object { ! $_.Options -match "ReadOnly" } | % { Remove-Item alias:$_ }} # https://stackoverflow.com/a/2816523
function remove-TempfilesNfolders { foreach ($folder in get-tempfilesNfolders) {Remove-Item $folder -force -recurse} }
function Sanatize-path { param( $path='G:\mhk',$replaceChar='_') if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $regex= "[^\)\w \\\ -:.åäöÅÄÖ %\ {,~$\(!@¤£é&=#§]" ; Get -ChildItem $path -Recurse | Where -Object {$_.name -match $regex} | rename -item -newName {$_.name -replace $regex,$replaceChar}}
function sed($path, $find, $replace) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } (Get-Content $path).replace("$find", $replace) | Set-Content $path }
function set-FileEncodingUtf8 ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } sc $path -encoding utf8 -value(gc $path) }
function set-x { Set-PSDebug -trace 2}
function set+x { Set-PSDebug -trace 0}
function sort-PathByLvl { param( $inputList) $inputList | Sort {($_ -split '\\').Count}, {$_} -Descending | select -object -first 2 | % { $error.clear() ; try { out -null -input (test -ModuleManifest $_ > '&2>&1' ) } catch { "Error" } ; if (!$error) { $_ } }}
function start-BrowserFlags { vivaldi "vivaldi://flags" } #todo: use standard browser instead of hardcoded
function start-notepad-profile { notepad $profile}
function string { process { $_ | Out-String -Stream } }
function touch($path) { "" | Out-File $path -Encoding ASCII }
function which($name) { Get-Command $name | Select-Object -ExpandProperty Definition } #should use more
Remove-Item alias:ls -ea SilentlyContinue ; function ls { Get-Childitem} # ls -al is musclememory by now so ignore all args for this "alias"
#------------------------------- Functions END -------------------------------
#------------------------------- Set alias BEGIN -------------------------------
$TAType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators") ; $TAType::Add('accelerators',$TAType)
set-alias accelerators [accelerators]::Get
set-alias edprofile start-Notepad-Profile
set-alias exp-pro open-ProfileFolder
set-alias kidStream get-childitem | out-string -stream
set-alias history (Get-PSReadlineOption).HistorySavePath
set-alias parameters get-parameters
set-alias start-su start-powershellAsAdmin
set-alias version $PSVersionTable # bash-like
Set-Alias cat Get-Content -Option AllScope
Set-Alias cd Set-Location -Option AllScope
Set-Alias clear Clear-Host -Option AllScope
Set-Alias cp Copy-Item -Option AllScope
Set-Alias history Get-History -Option AllScope
Set-Alias kill Stop-Process -Option AllScope
Set-Alias lp Out-Printer -Option AllScope
Set-Alias mv Move-Item -Option AllScope
Set-Alias ps Get-Process -Option AllScope
Set-Alias pwd Get-Location -Option AllScope
Set-Alias which Get-Command -Option AllScope
Set-Alias open Invoke-Item -Option AllScope
Set-Alias basename Split-Path -Option AllScope
Set-Alias realpath Resolve-Path -Option AllScope # cmd-like Set-Alias rm Remove-Item -Option AllScope
Set-Alias rmdir Remove-Item -Option AllScope
Set-Alias echo Write-Output -Option AllScope
Set-Alias cls Clear-Host -Option AllScope
Set-Alias chdir Set-Location -Option AllScope
Set-Alias copy Copy-Item -Option AllScope
Set-Alias del Remove-Item -Option AllScope
Set-Alias dir Get-Childitem -Option AllScope
Set-Alias erase Remove-Item -Option AllScope
Set-Alias move Move-Item -Option AllScope
Set-Alias rd Remove-Item -Option AllScope
Set-Alias ren Rename-Item -Option AllScope
Set-Alias set Set-Variable -Option AllScope
Set-Alias type Get-Content -Option AllScope
Set-Alias env Get-Environment -Option AllScope # custom aliases
Set-Alias flush-dns Clear-DnsClientCache -Option AllScope
Set-Alias touch Set-FileTime -Option AllScope
set-alias lsx get-Childnames -Option AllScope
set-alias filesinfolasstream read-childrenAsStream -Option AllScope
set-alias bcompare start-bc -Option AllScope
set-alias GitAdEPathAsSNB invoke-GitSubmoduleAdd -Option AllScope
set-alias GitUp invoke-GitLazy -Option AllScope
set-alias gitSilently invoke-GitLazySilently -Option AllScope
set-alias gitSingleRemote invoke-gitFetchOrig -Option AllScope
set-alias executeThis invoke-FuzzyWithEverything -Option AllScope
set-alias filesinfolasstream read-pathsAsStream -Option AllScope
set-alias everything invoke-Everything -Option AllScope
set-alias make invoke-Nmake -Option AllScope
set-alias MyAliases read-aliases -Option AllScope
set-alias OpenAsADmin invoke-powershellAsAdmin -Option AllScope
set-alias home open-here -Option AllScope
set-alias pastDo find-historyInvoke -Option AllScope
set-alias pastDoEdit find-historyAppendClipboard -Option AllScope
set-alias HistoryPath (Get-PSReadlineOption).HistorySavePath -Option AllScope
set-alias reboot exit-Nrenter -Option AllScope
set-alias browserflags start-BrowserFlags -Option AllScope
set-alias df get-volume -Option AllScope
set-alias printpaths read-EnvPaths -Option AllScope
set-alias reload initialize-profile -Option AllScope
set-alias uptime read-uptime -Option AllScope
set-alias getnic get-mac -Option AllScope # 1. 获取所有 Network Interface set-alias ll Get-ChildItem -Option AllScope
set-alias getip Get-IPv4Routes -Option AllScope
set-alias getip6 Get-IPv6Routes -Option AllScope
set-alias os-update Update-Packages -Option AllScope
set-alias remote invoke-gitRemote -Option AllScope
set-alias gitsplit subtree-split-rm-commit -Option AllScope
set-alias new-name Rename-Item -Option AllScope
set-alias set-name Rename-Item -Option AllScope
set-alias isFolder get-isFolder -Option AllScope
set-alias start-powershellAsAdmin invoke-powershellAsAdmin -Option AllScope
set-alias psVersion $PSVersionTable.PSVersion.Major -Option AllScope
#------------------------------- Set alias END -------------------------------
Write-Host "PSVersion: $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
Write-Host "PSEdition: $($PSVersionTable.PSEdition)"
Write-Host "Profile: $PSCommandPath"
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# src: https://gist.github.com/apfelchips/62a71500a0f044477698da71634ab87b
# New-Item $(Split-Path "$($PROFILE.CurrentUserCurrentHost)") -ItemType Directory -ea 0; Invoke-WebRequest -Uri "https://git.io/JYZTu" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
# ref: https://devblogs.microsoft.com/powershell/optimizing-your-profile/#measure-script
# ref: Powershell $? https://stackoverflow.com/a/55362991
# ref: Write-* https://stackoverflow.com/a/38527767
# Write-Host wrapper for Write-Information -InformationAction Continue
# define these environment variables if not set already and also provide them as PSVariables
if ( ( $null -eq $PSVersionTable.PSEdition) -or ($PSVersionTable.PSEdition -eq "Desktop") ) { $PSVersionTable.PSEdition = "Desktop" ;$IsWindows = $true }
if ( -not $IsWindows ) { function Test-IsAdmin { if ( (id -u) -eq 0 ) { return $true } return $false } }
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Clean-Object {
process {
$_.PSObject.Properties.Remove('PSComputerName')
$_.PSObject.Properties.Remove('RunspaceId')
$_.PSObject.Properties.Remove('PSShowComputerName')
}
#Where-Object { $_.PSObject.Properties.Value -ne $null}
}
function Get-Environment { # Get-Variable to show all Powershell Variables accessible via $
if ( $args.Count -eq 0 ) {
Get-Childitem env:
} elseif( $args.Count -eq 1 ) {
Start-Process (Get-Command $args[0]).Source
} else {
Start-Process (Get-Command $args[0]).Source -ArgumentList $args[1..($args.Count-1)]
}
}
function cf {
if ( $null -ne $(Get-Module PSFzf) ) {
Get-ChildItem . -Recurse -Attributes Directory | Invoke-Fzf | Set-Location
} else {
Write-Error "please install PSFzf"
}
}
if ( $(Test-CommandExists 'git') ) {
Set-Alias g git -Option AllScope
function git-root { $gitrootdir = (git rev-parse --show-toplevel) ; if ( $gitrootdir ) { Set-Location $gitrootdir } }
if ( $IsWindows ) {
function git-sh {
if ( $args.Count -eq 0 ) { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") -l
} else { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") $args }
}
function git-bash {
if ( $args.Count -eq 0 ) {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l
} else {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") $args
}
}
function git-vim { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l -c `'vim $args`' }
if ( -Not (Test-CommandExists 'sh') ){ Set-Alias sh git-sh -Option AllScope }
if ( -Not (Test-CommandExists 'bash') ){ Set-Alias bash git-bash -Option AllScope }
if ( -Not (Test-CommandExists 'vi') ){ Set-Alias vi git-vim -Option AllScope }
if ( -Not (Test-CommandExists 'vim') ){ Set-Alias vim git-vim -Option AllScope }
}
}
function Select-Value { # src: https://geekeefy.wordpress.com/2017/06/26/selecting-objects-by-value-in-powershell/
[Cmdletbinding()]
param(
[parameter(Mandatory=$true)] [String] $Value,
[parameter(ValueFromPipeline=$true)] $InputObject
)
process {
# Identify the PropertyName for respective matching Value, in order to populate it Default Properties
$Property = ($PSItem.properties.Where({$_.Value -Like "$Value"})).Name
If ( $Property ) {
# Create Property a set which includes the 'DefaultPropertySet' and Property for the respective 'Value' matched
$DefaultPropertySet = $PSItem.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
$TypeName = ($PSItem.PSTypenames)[0]
Get-TypeData $TypeName | Remove-TypeData
Update-TypeData -TypeName $TypeName -DefaultDisplayPropertySet ($DefaultPropertySet+$Property |Select-Object -Unique)
$PSItem | Where-Object {$_.properties.Value -like "$Value"}
}
}
}
function pause($message="Press any key to continue . . . ") {
Write-Host -NoNewline $message
$i=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183
while ($null -eq $k.VirtualKeyCode -or $i -Contains $k.VirtualKeyCode){
$k = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Write-Host ""
}
# native touch implementation
# src: https://ss64.com/ps/syntax-touch.html
function Set-FileTime {
param(
[string[]]$paths,
[bool]$only_modification = $false,
[bool]$only_access = $false
)
begin {
function updateFileSystemInfo([System.IO.FileSystemInfo]$fsInfo) {
$datetime = Get-Date
if ( $only_access ) {
$fsInfo.LastAccessTime = $datetime
} elseif ( $only_modification ) {
$fsInfo.LastWriteTime = $datetime
} else {
$fsInfo.CreationTime = $datetime
$fsInfo.LastWriteTime = $datetime
$fsInfo.LastAccessTime = $datetime
}
}
function touchExistingFile($arg) {
if ( $arg -is [System.IO.FileSystemInfo] ) {
updateFileSystemInfo($arg)
} else {
$resolvedPaths = Resolve-Path $arg
foreach ($rpath in $resolvedPaths) {
if ( Test-Path -type Container $rpath ) {
$fsInfo = New-Object System.IO.DirectoryInfo($rpath)
} else {
$fsInfo = New-Object System.IO.FileInfo($rpath)
}
updateFileSystemInfo($fsInfo)
}
}
}
function touchNewFile([string]$path) {
#$null > $path
Set-Content -Path $path -value $null;
}
}
process {
if ( $_ ) {
if ( Test-Path $_ ) {
touchExistingFile($_)
} else {
touchNewFile($_)
}
}
}
end {
if ( $paths ) {
foreach ( $path in $paths ) {
if ( Test-Path $path ) {
touchExistingFile($path)
} else {
touchNewFile($path)
}
}
}
}
}
if ( $IsWindows ) {
function stree($directory = $pwd) {
$gitrootdir = (Invoke-Command{Set-Location $args[0]; git rev-parse --show-toplevel 2>&1;} -ArgumentList $directory)
if ( Test-Path -Path "$gitrootdir\.git" -PathType Container) {
$newestExe = Get-Item "${env:ProgramFiles(x86)}\Atlassian\SourceTree\SourceTree.exe" | select -Last 1
Write-Debug "Opening $gitrootdir with $newestExe"
Start-Process -filepath $newestExe -ArgumentList "-f `"$gitrootdir`" log"
} else {
Write-Error "git directory not found"
}
}
if ( "${env:ChocolateyInstall}" -eq "" ) {
function Install-Chocolatey {
if (Get-Command choco -ErrorAction SilentlyContinue) {
Write-Error "chocolatey already installed!"
} else {
Start-Process (Get-HostExecutable) -ArgumentList "-Command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1') -verb RunAs"
}
}
else {
function choco { Start-Process (Get-HostExecutable) -ArgumentList "-noProfile -noLogo -Command choco.exe ${args}; pause" -verb runAs }
}
}
}
function Get-HostExecutable {
if ( $PSVersionTable.PSEdition -eq "Core" ) {
$ConsoleHostExecutable = (get-command pwsh).Source
} else {
$ConsoleHostExecutable = (get-command powershell).Source
}
return $ConsoleHostExecutable
}
# don't override chocolatey sudo or unix sudo
if ( -not $(Test-CommandExists 'sudo') ) {
function sudo() {
if ( $args.Length -eq 0 ) {
Start-Process $(Get-HostExecutable) -verb "runAs"
} elseif ( $args.Length -eq 1 ) {
Start-Process $args[0] -verb "runAs"
} else {
Start-Process $args[0] -ArgumentList $args[1..$args.Length] -verb "runAs"
}
}
}
# Helper Functions
#######################################################
function uptimef {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function unzipf ($path) {
$dirname = (Get-Item $path).Basename
echo("Extracting", $path, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $path -OutputPath $dirname -ShowProgress
}
# already expanded to save time https://github.com/nvbn/thefuck/wiki/Shell-aliases#powershell
if ( $(Test-CommandExists 'thefuck') ) {
function fuck {
$PYTHONIOENCODING_BKP=$env:PYTHONIOENCODING
$env:PYTHONIOENCODING="utf-8"
$history = (Get-History -Count 1).CommandLine
if (-not [string]::IsNullOrWhiteSpace($history)) {
$fuck = $(thefuck $args $history)
if ( -not [string]::IsNullOrWhiteSpace($fuck) ) {
if ( $fuck.StartsWith("echo") ) { $fuck = $fuck.Substring(5) } else { iex "$fuck" }
}
}
[Console]::ResetColor()
$env:PYTHONIOENCODING=$PYTHONIOENCODING_BKP
}
Set-Alias f fuck -Option AllScope
}
# hacks for old powerhsell versions
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
function Get-ExitBoolean($command) { & $command | Out-Null; $?} ; Set-Alias geb Get-ExitBoolean # fixed: https://github.com/PowerShell/PowerShell/pull/9849
function Use-Default # $var = d $Value : "DefaultValue" eg. ternary # fixed: https://toastit.dev/2019/09/25/ternary-operator-powershell-7/
{
for ($i = 1; $i -lt $args.Count; $i++){
if ($args[$i] -eq ":"){
$coord = $i; break
}
}
if ($coord -eq 0) {
throw new System.Exception "No operator!"
}
if ($args[$coord - 1] -eq ""){
$toReturn = $args[$coord + 1]
} else {
$toReturn = $args[$coord -1]
}
return $toReturn
}
Set-Alias d Use-Default
}
if ( $IsWindows ) {
# src: http://serverfault.com/questions/95431
function Test-IsAdmin { $user = [Security.Principal.WindowsIdentity]::GetCurrent(); return $(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator); }
function Reopen-here { Get-Process explorer | Stop-Process Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Get-Process explorer | Stop-Process'" -verb "runAs"}
function Reset-Spooler { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Stop-Service -Name Spooler -Force; Get-Item ${env:SystemRoot}\System32\spool\PRINTERS\* | Remove-Item -Force -Recurse; Start-Service -Name Spooler'" -verb "runAs" }
function subl { Start-Process "${Env:ProgramFiles}\Sublime Text\subl.exe" -ArgumentList $args -WindowStyle Hidden } # hide subl shim script
function get-tempfilesNfolders { foreach ($folder in @('C:\Windows\Temp\*', 'C:\Documents and Settings\*\Local Settings\temp\*', 'C:\Users\*\Appdata\Local\Temp\*', 'C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*', 'C:\Windows\SoftwareDistribution\Download', 'C:\Windows\System32\FNTCACHE.DAT')) {$_} }
function Export-Regestrykey { param ( $reg = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\' ,$outFile = 'H:\hkcu-regbackup.txt' ) get-childitem -path $reg | out-file $outFile }
function start-bc ($REMOTE,$LOCAL,$BASE,$MERGED) { cmd /c "${Env:ProgramFiles}\BeondCompare4\BComp.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" } #todo: replace hardcode with global variable pointing to path
function start-cygwin { param ( $cygwinpath = "${Env:ProgramFiles}\cygwin64\" ) ."$cygwinpath\Cygwin.bat" }
function Compare-ImagesMetadata { param ( $exifToolPath = "${Env:ProgramFiles}\geosetter\tools\" ,$inputA = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front (2).jpg" ,$inputB = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front.jpg" ) ; $set1 = .\exiftool.exe -a -u -g1 $inputA ; $set2 = .\exiftool.exe -a -u -g1 $inputB ; Compare-Object $set1 $set2 | select -ExpandProperty inputobject }
function new-SymbolicLink { param ( $where = 'H:\mina grejer\Till Github' ,$from = 'H:\mina grejer\Project shelf\Till Github' ) New-Item -Path $where -ItemType SymbolicLink -Value $from }
}
if (Test-CommandExists 'search-Everything')
{
function invoke-Everything([string]$filter) { @(Search-Everything -filter $filter -global) }
function invoke-FuzzyWithEverything($searchstring) { menu @(everything "ext:exe $searchString") | %{& $_ } } #use whatpulse db first, then everything #todo: sort by rescent use #use everything to find executable for fast execution
function Every-execute ($inputx) { $filter = "ext:exe $inputx" ; $filter } #& (Every-Menu $filter)
function Every-AsHashMap { param( $filter = 'ext:psd1 \module') $q = @{} ; everything $filter | %{@{ name = (get -item $_).name ; time=(get -item $_).LastWriteTime ; path=(get -item $_) } } | sort -object -property time | %{ $q[$_.name] = $_.path } ; $q | select -property values}
function Every-execute { param( $filter = 'regex:".*\\data\\[^\\]*.ahk"',$navigate=$true) Every-Menu | % { if($navigate) {cd ($_ | split -path -parent)} ; . $_ } }
function Every-invoke-expression { param( $psFileFilter = 'convert-xlsx-to-csv.ps1') . ( everythnig $psFileFilter | select -first 1) } ; invoke-expression "ExcelToCsv -File 'D:\unsorted\fannyUtskick.xlsx'"
function Every-Explore { param( $filter = 'ext:exe lasso') ; Every-Menu $filter | % { $path = if(!( Test-Path $_ -PathType Container)) { $_ | split-path -leaf } else {$_} ; explorer $path } }
function Every-Menu { param( $filter) $a= @(everything $filter) ; if($a.count -eq 1) {$a} else {menu $a} }
function Every-AddType { param( $filter) $a = Every-Menu $filter ; Add-Type -Path $a }
}
if (Test-CommandExists 'git')
{ #todo: move to git aliases
function invoke-gitCheckout () { & git checkout $args }
function invoke-gitFetchOrig { git fetch origin }
Function invoke-GitLazy($path,$message) { cd $path ; git lazy $message } ;
Function invoke-GitLazySilently {Out-File -FilePath .\lazy.log -inputObject (invoke-GitLazy 'AutoCommit' 2>&1 )} ; #todo: parameterize #todo: rename to more descriptive #todo: breakout
function invoke-gitRemote { param ($subCommand = 'get-url',$name = "origin" ) git remote $subCommand $name }
Function invoke-GitSubmoduleAdd([string]$leaf,[string]$remote,[string]$branch) { git submodule add -f --name $leaf -- $remote $branch ; git commit -am $leaf+$remote+$branch } ; #todo: move to git aliases #Git Ad $leaf as submodule from $remote and branch $branch
function TortCommit ($path) { & 'C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe' /command:commit }
function TortCommit ($path,$message) { & 'C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe' /command:commit /path:$path /logmsg:$messsage }
}
if ( $null -ne $(Get-Module PSReadline -ea SilentlyContinue)) {
function find-historyAppendClipboard($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{ Set-Clipboard -Value $_ }} #search history of past expressions and adds to clipboard
function find-historyInvoke($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{Invoke-Expression $_ } } #search history of past expressions and invokes it, doesn't register the expression itself in history, but the pastDo expression.
}
function split-fileByLineNr { param( $pathName = '.\gron.csv',$OutputFilenamePattern = 'output_done_' , $LineLimit = 60) ;
$ext = $pathName | split-path -Extension
$inputx = Get-Content ;
$line = 0 ;
$i = 0 ;
$path = 0 ;
$start = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq $LineLimit -Or $line -eq $inputx.Length) {
$path++ ;
$pathname = "$OutputFilenamePattern$path$ext" ;
$inputx[$start..($line - 1)] | Out -File $pathname -Force ;
$start = $line ;
$i = 0 ;
Write-Host "$pathname" ;
} ;
$i++ ;
$line++
}
}
function split-fileByMatch($pathName , $regex) { #param( $pathName = 'C:\Users\crbk01\Documents\WindowsPowerShell\snipps\Modules\Todo SplitUp.psm1' , $regex = '(?<=function\s)[^\s\(]*') ;
$ext = ($pathName | split-path -Extension)
$parent = ($pathName | split-path -Parent)
$OriginalName = ($pathName | split-path -LeafBase)
$inputx = Get-Content $pathName; $line = 0 ; $i = 0 ; $start = @(select-string -path $pathName -pattern $regex ) | select linenumber ; $LineLimit = $start | select -Skip 1 ; $names = @() ; [regex]::matches($inputx,$regex).groups.value | %{$names+= $_ }
$occurence = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq ([int]$LineLimit[$occurence].linenumber -1) -Or $line -eq $inputx.Length)
{
$currentName = $names[$occurence];
$pathname = Join-Path -path $parent -childPath "$OriginalName-$currentName$ext" ;
$u = ([int]$start[$occurence].linenumber -1)
$inputx[$u..($line - 1)] > $pathname
$occurence++ ;
Write-Host "$u..($line - 1)$pathname" ;
};
$i++ ;
$line++
}
}
#function aliasCode { & $env:code }
function .. { Set-Location ".." }
function .... { Set-Location (Join-Path -Path ".." -ChildPath "..") }
function all { process { $_ | Select-Object * } } # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#piping-objects-to-functions
function cdc { Set-Location "$XDG_CONFIG_HOME" }
function cdcheat { Set-Location "$CHEATS_DIR" }
function cdd { Set-Location "$DESKTOP_DIR" }
function cddd { Set-Location "$DEVEL_DIR" }
function cddev { Set-Location "$DEVEL_DIR" }
function cdn { Set-Location "$NOTES_DIR" }
function cdports { Set-Location "$PORTS_DIR" }
function cdt { Set-Location "$TODO_DIR" }
function clear-Days_Back { param( $path = "C:\Support\SQLBac\" ,$Daysback = "0" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item }
function ConvertFrom-Bytes { param( [string]$bytes, [string]$savepath ) $dir = Split-Path $savepath if (!(Test-Path $dir)) { md $dir | Out-Null } [convert]::FromBase64String($bytes) | Set-Content $savepath -Encoding Byte }
function ConvertTo-Bytes ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } [convert]::ToBase64String((Get-Content $path -Encoding Byte)) }
function df { get-volume }
function enter-dir { param( $path = '%USERPROFILE%\Desktop\' ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Set-Location $Path }; #if no param, navigate to desktop # 5. 更改工作目录 # 输入要切换到的路径 # 用法示例:cd C:/ # 默认路径:D 盘的桌面
function exit-Nrenter { shutdown /r } #reboot
function export($name, $value) { set-item -force -path "env:$name" -value $value; }
function foldercontain($folder,$name) { $q = get-childitem $folder; return $q -contains $name }
function Get-AllNic { Get-NetAdapter | Sort-Object -Property MacAddress } # 1. 获取所有 Network Interface
function get-Childnames { param( $path = $pwd) (Get-ChildItem -path $path).Name ; Write-Host("") }; # 3. 查看目录 ls & ll
function Get-DefaultAliases { Get-Alias | Where-Object { $_.Options -match "ReadOnly" }}
function get-envVar { Get-Childitem -Path Env:*}
function get-EspHeader { param( $path = '.\FormiD.esp', $max = 13,$headerSize = 3 ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $q = ((( -join (gc $path | select -first $headerSize)) -replace "[^\w. ] {1,}","å").split("å") -match "^.*[.](esp |esm)$") ; $q | ? {($q | select -first $max) -NotContains $_}}
function get-historyPath { (Get-PSReadlineOption).HistorySavePath }
function Get-IPv4Routes { Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } # 2. 获取 IPv4 关键路由
function Get-IPv6Routes { Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } # 3. 获取 IPv6 关键路由
function get-isFolder {$PSBoundParameters -is [system.in.folder]}
function get-parameters { Get-Member -Parameter *}
function get-RegInstallpaths { Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | % { Get -ItemProperty $_.PsPath } | Select DisplayName,InstallLocation }
function get-whatpulse { param( $program,$path) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $query = "select rightstr(path,instr(reverse(path),'/') -1) exe,path from (select max(path) path,max(cast(replace(version,'.','') as integer)) version from applications group by case when online_app_id = 0 then name else online_app_id end)" ; $adapter = newSqliteConnection -source (Everything 'whatpulse.db')[0] -query $query ; $b=@($data.item('exe')) ; $a = @($data.item('path')) ; $i=0 ; while($i -lt $a.Length) {$res[$b[$i]]=$a[$i] ; $i++ } ; $res | where { $_.name -match $program -and $_.path -match $path}}
function grep { process { $_ | Select-String -Pattern $args } } # function grep($regex, $dir) { if ( $dir ) { ls $dir | select-string $regex return } $input | select-string $regex }
function grepv($regex) { $input | ? { !$_.Contains($regex) } }
function Initialize-Profile { . $PROFILE.CurrentUserCurrentHost} #function initialize-profile { & $profile } #reload-profile is an unapproved verb.
function invoke-Nmake { nmake.exe $args -nologo }; # 1. 编译函数 make
function invoke-powershellAsAdmin { Start-Process powershell -Verb runAs } #new ps OpenAsADmin
function join-ByRuncunfig { param( $prefix='[$]APPLICATION_CONFIG_DIR[$][/]'
,$refixReplace='C:\Users\crbk01\AppData\Roaming\JetBrains\DataGrip2021.1\',
$runconfig="TillMinaMedelanden.run.xml",$output='runConfig/Combined.sql') [xml]$xml=get -content $runConfig -Encoding UTF8 ; $xml.component.configuration.'script -file' | %
{$_.value -replace $prefix, ($prefixReplace -replace '\\','/')} | % {" - -:$_"
; get -content -path $_ -Encoding UTF8 ; "go" } >> $output }
function list { process { $_ | Format-List * } } # fl is there by default
function man { Get-Help $args[0] | out-host -paging }
function md { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function measure-ExtOccurenseRecursivly { param( $path = "D:\Project Shelf\MapBasic" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Get-ChildItem -Path $path -Recurse -File | group Extension -NoElement | sort Count -Descending | select -Property name }
function measure-words { param( $inputStream = (Get -ChildItem | % { Get -Content $_.name }), $regex="[^a-z0-9_-]") $hash = @{} ; $a="" ; $inputStream | % {$a+=$_} ; $a -split $regex | % { $_.tolower()} | % {$hash[$_]++} ; $sorted= % {$hash.GetEnumerator() | sort -object {[int]$_.value}} ; return $sorted} ; $sorted | where{$_.name -notmatch "^\d+$"} | where{$_.name.length -gt 4 }
function mkdir { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function My-Scripts { Get-Command -CommandType externalscript }
function open-here { param( $Path = $pwd ) Invoke-Item $Path }; # 4. 打开当前工作目录 # 输入要打开的路径 # 用法示例:open C:\ # 默认路径:当前工作文件夹
function open-ProfileFolder { explorer (split-path -path $profile -parent)}
function pgrep($name) { Get-Process $name }
function pkill($name) { Get-Process $name -ErrorAction SilentlyContinue | kill }
function pull () { & get pull $args }
function read-aliases { Get-Alias | Where-Object { $_.Options -notmatch "ReadOnly" }}
function read-EnvPaths { ($Env:Path).Split(";") }
function read-headOfFile { param( $linr = 10, $path ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } gc -Path $path -TotalCount $linr }
function read-json { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSCustomObject] $input ) $json = [ordered]@{}; ($input).PSObject.Properties | % { $json[$_.Name] = $_.Value } $json.SyncRoot }
function read-paramNaliases ($command) { (Get-Command $command).parameters.values | select name, @{n='aliases';e={$_.aliases}} }
function read-pathsAsStream { get-childitem | out-string -stream } # filesInFolAsStream ;
function read-uptime { Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime'; EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}} } #doesn't psreadline module implement this already?
function Remove-CustomAliases { Get-Alias | Where-Object { ! $_.Options -match "ReadOnly" } | % { Remove-Item alias:$_ }} # https://stackoverflow.com/a/2816523
function remove-TempfilesNfolders { foreach ($folder in get-tempfilesNfolders) {Remove-Item $folder -force -recurse} }
function Sanatize-path { param( $path='G:\mhk',$replaceChar='_') if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $regex= "[^\)\w \\\ -:.åäöÅÄÖ %\ {,~$\(!@¤£é&=#§]" ; Get -ChildItem $path -Recurse | Where -Object {$_.name -match $regex} | rename -item -newName {$_.name -replace $regex,$replaceChar}}
function sed($path, $find, $replace) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } (Get-Content $path).replace("$find", $replace) | Set-Content $path }
function set-FileEncodingUtf8 ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } sc $path -encoding utf8 -value(gc $path) }
function set-x { Set-PSDebug -trace 2}
function set+x { Set-PSDebug -trace 0}
function sort-PathByLvl { param( $inputList) $inputList | Sort {($_ -split '\\').Count}, {$_} -Descending | select -object -first 2 | % { $error.clear() ; try { out -null -input (test -ModuleManifest $_ > '&2>&1' ) } catch { "Error" } ; if (!$error) { $_ } }}
function start-BrowserFlags { vivaldi "vivaldi://flags" } #todo: use standard browser instead of hardcoded
function start-notepad-profile { notepad $profile}
function string { process { $_ | Out-String -Stream } }
function touch($path) { "" | Out-File $path -Encoding ASCII }
function which($name) { Get-Command $name | Select-Object -ExpandProperty Definition } #should use more
Remove-Item alias:ls -ea SilentlyContinue ; function ls { Get-Childitem} # ls -al is musclememory by now so ignore all args for this "alias"
#------------------------------- Functions END -------------------------------
#------------------------------- Set alias BEGIN -------------------------------
$TAType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators") ; $TAType::Add('accelerators',$TAType)
set-alias accelerators [accelerators]::Get
set-alias edprofile start-Notepad-Profile
set-alias exp-pro open-ProfileFolder
set-alias kidStream get-childitem | out-string -stream
set-alias history (Get-PSReadlineOption).HistorySavePath
set-alias parameters get-parameters
set-alias start-su start-powershellAsAdmin
set-alias version $PSVersionTable # bash-like
Set-Alias cat Get-Content -Option AllScope
Set-Alias cd Set-Location -Option AllScope
Set-Alias clear Clear-Host -Option AllScope
Set-Alias cp Copy-Item -Option AllScope
Set-Alias history Get-History -Option AllScope
Set-Alias kill Stop-Process -Option AllScope
Set-Alias lp Out-Printer -Option AllScope
Set-Alias mv Move-Item -Option AllScope
Set-Alias ps Get-Process -Option AllScope
Set-Alias pwd Get-Location -Option AllScope
Set-Alias which Get-Command -Option AllScope
Set-Alias open Invoke-Item -Option AllScope
Set-Alias basename Split-Path -Option AllScope
Set-Alias realpath Resolve-Path -Option AllScope # cmd-like Set-Alias rm Remove-Item -Option AllScope
Set-Alias rmdir Remove-Item -Option AllScope
Set-Alias echo Write-Output -Option AllScope
Set-Alias cls Clear-Host -Option AllScope
Set-Alias chdir Set-Location -Option AllScope
Set-Alias copy Copy-Item -Option AllScope
Set-Alias del Remove-Item -Option AllScope
Set-Alias dir Get-Childitem -Option AllScope
Set-Alias erase Remove-Item -Option AllScope
Set-Alias move Move-Item -Option AllScope
Set-Alias rd Remove-Item -Option AllScope
Set-Alias ren Rename-Item -Option AllScope
Set-Alias set Set-Variable -Option AllScope
Set-Alias type Get-Content -Option AllScope
Set-Alias env Get-Environment -Option AllScope # custom aliases
Set-Alias flush-dns Clear-DnsClientCache -Option AllScope
Set-Alias touch Set-FileTime -Option AllScope
set-alias lsx get-Childnames -Option AllScope
set-alias filesinfolasstream read-childrenAsStream -Option AllScope
set-alias bcompare start-bc -Option AllScope
set-alias GitAdEPathAsSNB invoke-GitSubmoduleAdd -Option AllScope
set-alias GitUp invoke-GitLazy -Option AllScope
set-alias gitSilently invoke-GitLazySilently -Option AllScope
set-alias gitSingleRemote invoke-gitFetchOrig -Option AllScope
set-alias executeThis invoke-FuzzyWithEverything -Option AllScope
set-alias filesinfolasstream read-pathsAsStream -Option AllScope
set-alias everything invoke-Everything -Option AllScope
set-alias make invoke-Nmake -Option AllScope
set-alias MyAliases read-aliases -Option AllScope
set-alias OpenAsADmin invoke-powershellAsAdmin -Option AllScope
set-alias home open-here -Option AllScope
set-alias pastDo find-historyInvoke -Option AllScope
set-alias pastDoEdit find-historyAppendClipboard -Option AllScope
set-alias HistoryPath (Get-PSReadlineOption).HistorySavePath -Option AllScope
set-alias reboot exit-Nrenter -Option AllScope
set-alias browserflags start-BrowserFlags -Option AllScope
set-alias df get-volume -Option AllScope
set-alias printpaths read-EnvPaths -Option AllScope
set-alias reload initialize-profile -Option AllScope
set-alias uptime read-uptime -Option AllScope
set-alias getnic get-mac -Option AllScope # 1. 获取所有 Network Interface set-alias ll Get-ChildItem -Option AllScope
set-alias getip Get-IPv4Routes -Option AllScope
set-alias getip6 Get-IPv6Routes -Option AllScope
set-alias os-update Update-Packages -Option AllScope
set-alias remote invoke-gitRemote -Option AllScope
set-alias gitsplit subtree-split-rm-commit -Option AllScope
set-alias new-name Rename-Item -Option AllScope
set-alias set-name Rename-Item -Option AllScope
set-alias isFolder get-isFolder -Option AllScope
set-alias start-powershellAsAdmin invoke-powershellAsAdmin -Option AllScope
set-alias psVersion $PSVersionTable.PSVersion.Major -Option AllScope
#------------------------------- Set alias END -------------------------------
Write-Host "PSVersion: $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
Write-Host "PSEdition: $($PSVersionTable.PSEdition)"
Write-Host "Profile: $PSCommandPath"
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# src: https://gist.github.com/apfelchips/62a71500a0f044477698da71634ab87b
# New-Item $(Split-Path "$($PROFILE.CurrentUserCurrentHost)") -ItemType Directory -ea 0; Invoke-WebRequest -Uri "https://git.io/JYZTu" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
# ref: https://devblogs.microsoft.com/powershell/optimizing-your-profile/#measure-script
# ref: Powershell $? https://stackoverflow.com/a/55362991
# ref: Write-* https://stackoverflow.com/a/38527767
# Write-Host wrapper for Write-Information -InformationAction Continue
# define these environment variables if not set already and also provide them as PSVariables
if ( ( $null -eq $PSVersionTable.PSEdition) -or ($PSVersionTable.PSEdition -eq "Desktop") ) { $PSVersionTable.PSEdition = "Desktop" ;$IsWindows = $true }
if ( -not $IsWindows ) { function Test-IsAdmin { if ( (id -u) -eq 0 ) { return $true } return $false } }
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Clean-Object {
process {
$_.PSObject.Properties.Remove('PSComputerName')
$_.PSObject.Properties.Remove('RunspaceId')
$_.PSObject.Properties.Remove('PSShowComputerName')
}
#Where-Object { $_.PSObject.Properties.Value -ne $null}
}
function Get-Environment { # Get-Variable to show all Powershell Variables accessible via $
if ( $args.Count -eq 0 ) {
Get-Childitem env:
} elseif( $args.Count -eq 1 ) {
Start-Process (Get-Command $args[0]).Source
} else {
Start-Process (Get-Command $args[0]).Source -ArgumentList $args[1..($args.Count-1)]
}
}
function cf {
if ( $null -ne $(Get-Module PSFzf) ) {
Get-ChildItem . -Recurse -Attributes Directory | Invoke-Fzf | Set-Location
} else {
Write-Error "please install PSFzf"
}
}
if ( $(Test-CommandExists 'git') ) {
Set-Alias g git -Option AllScope
function git-root { $gitrootdir = (git rev-parse --show-toplevel) ; if ( $gitrootdir ) { Set-Location $gitrootdir } }
if ( $IsWindows ) {
function git-sh {
if ( $args.Count -eq 0 ) { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") -l
} else { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\sh") $args }
}
function git-bash {
if ( $args.Count -eq 0 ) {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l
} else {
. $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") $args
}
}
function git-vim { . $(Join-Path -Path $(Split-Path -Path $(Get-Command git).Source) -ChildPath "..\bin\bash") -l -c `'vim $args`' }
if ( -Not (Test-CommandExists 'sh') ){ Set-Alias sh git-sh -Option AllScope }
if ( -Not (Test-CommandExists 'bash') ){ Set-Alias bash git-bash -Option AllScope }
if ( -Not (Test-CommandExists 'vi') ){ Set-Alias vi git-vim -Option AllScope }
if ( -Not (Test-CommandExists 'vim') ){ Set-Alias vim git-vim -Option AllScope }
}
}
function Select-Value { # src: https://geekeefy.wordpress.com/2017/06/26/selecting-objects-by-value-in-powershell/
[Cmdletbinding()]
param(
[parameter(Mandatory=$true)] [String] $Value,
[parameter(ValueFromPipeline=$true)] $InputObject
)
process {
# Identify the PropertyName for respective matching Value, in order to populate it Default Properties
$Property = ($PSItem.properties.Where({$_.Value -Like "$Value"})).Name
If ( $Property ) {
# Create Property a set which includes the 'DefaultPropertySet' and Property for the respective 'Value' matched
$DefaultPropertySet = $PSItem.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
$TypeName = ($PSItem.PSTypenames)[0]
Get-TypeData $TypeName | Remove-TypeData
Update-TypeData -TypeName $TypeName -DefaultDisplayPropertySet ($DefaultPropertySet+$Property |Select-Object -Unique)
$PSItem | Where-Object {$_.properties.Value -like "$Value"}
}
}
}
function pause($message="Press any key to continue . . . ") {
Write-Host -NoNewline $message
$i=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183
while ($null -eq $k.VirtualKeyCode -or $i -Contains $k.VirtualKeyCode){
$k = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Write-Host ""
}
# native touch implementation
# src: https://ss64.com/ps/syntax-touch.html
function Set-FileTime {
param(
[string[]]$paths,
[bool]$only_modification = $false,
[bool]$only_access = $false
)
begin {
function updateFileSystemInfo([System.IO.FileSystemInfo]$fsInfo) {
$datetime = Get-Date
if ( $only_access ) {
$fsInfo.LastAccessTime = $datetime
} elseif ( $only_modification ) {
$fsInfo.LastWriteTime = $datetime
} else {
$fsInfo.CreationTime = $datetime
$fsInfo.LastWriteTime = $datetime
$fsInfo.LastAccessTime = $datetime
}
}
function touchExistingFile($arg) {
if ( $arg -is [System.IO.FileSystemInfo] ) {
updateFileSystemInfo($arg)
} else {
$resolvedPaths = Resolve-Path $arg
foreach ($rpath in $resolvedPaths) {
if ( Test-Path -type Container $rpath ) {
$fsInfo = New-Object System.IO.DirectoryInfo($rpath)
} else {
$fsInfo = New-Object System.IO.FileInfo($rpath)
}
updateFileSystemInfo($fsInfo)
}
}
}
function touchNewFile([string]$path) {
#$null > $path
Set-Content -Path $path -value $null;
}
}
process {
if ( $_ ) {
if ( Test-Path $_ ) {
touchExistingFile($_)
} else {
touchNewFile($_)
}
}
}
end {
if ( $paths ) {
foreach ( $path in $paths ) {
if ( Test-Path $path ) {
touchExistingFile($path)
} else {
touchNewFile($path)
}
}
}
}
}
if ( $IsWindows ) {
function stree($directory = $pwd) {
$gitrootdir = (Invoke-Command{Set-Location $args[0]; git rev-parse --show-toplevel 2>&1;} -ArgumentList $directory)
if ( Test-Path -Path "$gitrootdir\.git" -PathType Container) {
$newestExe = Get-Item "${env:ProgramFiles(x86)}\Atlassian\SourceTree\SourceTree.exe" | select -Last 1
Write-Debug "Opening $gitrootdir with $newestExe"
Start-Process -filepath $newestExe -ArgumentList "-f `"$gitrootdir`" log"
} else {
Write-Error "git directory not found"
}
}
if ( "${env:ChocolateyInstall}" -eq "" ) {
function Install-Chocolatey {
if (Get-Command choco -ErrorAction SilentlyContinue) {
Write-Error "chocolatey already installed!"
} else {
Start-Process (Get-HostExecutable) -ArgumentList "-Command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1') -verb RunAs"
}
}
else {
function choco { Start-Process (Get-HostExecutable) -ArgumentList "-noProfile -noLogo -Command choco.exe ${args}; pause" -verb runAs }
}
}
}
function Get-HostExecutable {
if ( $PSVersionTable.PSEdition -eq "Core" ) {
$ConsoleHostExecutable = (get-command pwsh).Source
} else {
$ConsoleHostExecutable = (get-command powershell).Source
}
return $ConsoleHostExecutable
}
# don't override chocolatey sudo or unix sudo
if ( -not $(Test-CommandExists 'sudo') ) {
function sudo() {
if ( $args.Length -eq 0 ) {
Start-Process $(Get-HostExecutable) -verb "runAs"
} elseif ( $args.Length -eq 1 ) {
Start-Process $args[0] -verb "runAs"
} else {
Start-Process $args[0] -ArgumentList $args[1..$args.Length] -verb "runAs"
}
}
}
# Helper Functions
#######################################################
function uptimef {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function find-file($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function unzipf ($path) {
$dirname = (Get-Item $path).Basename
echo("Extracting", $path, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $path -OutputPath $dirname -ShowProgress
}
# already expanded to save time https://github.com/nvbn/thefuck/wiki/Shell-aliases#powershell
if ( $(Test-CommandExists 'thefuck') ) {
function fuck {
$PYTHONIOENCODING_BKP=$env:PYTHONIOENCODING
$env:PYTHONIOENCODING="utf-8"
$history = (Get-History -Count 1).CommandLine
if (-not [string]::IsNullOrWhiteSpace($history)) {
$fuck = $(thefuck $args $history)
if ( -not [string]::IsNullOrWhiteSpace($fuck) ) {
if ( $fuck.StartsWith("echo") ) { $fuck = $fuck.Substring(5) } else { iex "$fuck" }
}
}
[Console]::ResetColor()
$env:PYTHONIOENCODING=$PYTHONIOENCODING_BKP
}
Set-Alias f fuck -Option AllScope
}
# hacks for old powerhsell versions
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
function Get-ExitBoolean($command) { & $command | Out-Null; $?} ; Set-Alias geb Get-ExitBoolean # fixed: https://github.com/PowerShell/PowerShell/pull/9849
function Use-Default # $var = d $Value : "DefaultValue" eg. ternary # fixed: https://toastit.dev/2019/09/25/ternary-operator-powershell-7/
{
for ($i = 1; $i -lt $args.Count; $i++){
if ($args[$i] -eq ":"){
$coord = $i; break
}
}
if ($coord -eq 0) {
throw new System.Exception "No operator!"
}
if ($args[$coord - 1] -eq ""){
$toReturn = $args[$coord + 1]
} else {
$toReturn = $args[$coord -1]
}
return $toReturn
}
Set-Alias d Use-Default
}
if ( $IsWindows ) {
# src: http://serverfault.com/questions/95431
function Test-IsAdmin { $user = [Security.Principal.WindowsIdentity]::GetCurrent(); return $(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator); }
function Reopen-here { Get-Process explorer | Stop-Process Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Get-Process explorer | Stop-Process'" -verb "runAs"}
function Reset-Spooler { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command 'Stop-Service -Name Spooler -Force; Get-Item ${env:SystemRoot}\System32\spool\PRINTERS\* | Remove-Item -Force -Recurse; Start-Service -Name Spooler'" -verb "runAs" }
function subl { Start-Process "${Env:ProgramFiles}\Sublime Text\subl.exe" -ArgumentList $args -WindowStyle Hidden } # hide subl shim script
function get-tempfilesNfolders { foreach ($folder in @('C:\Windows\Temp\*', 'C:\Documents and Settings\*\Local Settings\temp\*', 'C:\Users\*\Appdata\Local\Temp\*', 'C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*', 'C:\Windows\SoftwareDistribution\Download', 'C:\Windows\System32\FNTCACHE.DAT')) {$_} }
function Export-Regestrykey { param ( $reg = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\' ,$outFile = 'H:\hkcu-regbackup.txt' ) get-childitem -path $reg | out-file $outFile }
function start-bc ($REMOTE,$LOCAL,$BASE,$MERGED) { cmd /c "${Env:ProgramFiles}\BeondCompare4\BComp.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" } #todo: replace hardcode with global variable pointing to path
function start-cygwin { param ( $cygwinpath = "${Env:ProgramFiles}\cygwin64\" ) ."$cygwinpath\Cygwin.bat" }
function Compare-ImagesMetadata { param ( $exifToolPath = "${Env:ProgramFiles}\geosetter\tools\" ,$inputA = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front (2).jpg" ,$inputB = "E:\Pictures\Badges & Signs & Shablon Art\00 - soulcripple front.jpg" ) ; $set1 = .\exiftool.exe -a -u -g1 $inputA ; $set2 = .\exiftool.exe -a -u -g1 $inputB ; Compare-Object $set1 $set2 | select -ExpandProperty inputobject }
function new-SymbolicLink { param ( $where = 'H:\mina grejer\Till Github' ,$from = 'H:\mina grejer\Project shelf\Till Github' ) New-Item -Path $where -ItemType SymbolicLink -Value $from }
}
if (Test-CommandExists 'search-Everything')
{
function invoke-Everything([string]$filter) { @(Search-Everything -filter $filter -global) }
function invoke-FuzzyWithEverything($searchstring) { menu @(everything "ext:exe $searchString") | %{& $_ } } #use whatpulse db first, then everything #todo: sort by rescent use #use everything to find executable for fast execution
function Every-execute ($inputx) { $filter = "ext:exe $inputx" ; $filter } #& (Every-Menu $filter)
function Every-AsHashMap { param( $filter = 'ext:psd1 \module') $q = @{} ; everything $filter | %{@{ name = (get -item $_).name ; time=(get -item $_).LastWriteTime ; path=(get -item $_) } } | sort -object -property time | %{ $q[$_.name] = $_.path } ; $q | select -property values}
function Every-execute { param( $filter = 'regex:".*\\data\\[^\\]*.ahk"',$navigate=$true) Every-Menu | % { if($navigate) {cd ($_ | split -path -parent)} ; . $_ } }
function Every-Load { param( $psFileFilter = 'convert-xlsx-to-csv.ps1') . ( everythnig $psFileFilter | select -first 1) } ; invoke-expression "ExcelToCsv -File 'D:\unsorted\fannyUtskick.xlsx'"
function Every-Explore { param( $filter = 'ext:exe lasso') ; Every-Menu $filter | % { $path = if(!( Test-Path $_ -PathType Container)) { $_ | split-path -leaf } else {$_} ; explorer $path } }
function Every-Menu { param( $filter) $a= @(everything $filter) ; if($a.count -eq 1) {$a} else {menu $a} }
}
if (Test-CommandExists 'git')
{ #todo: move to git aliases
function invoke-gitCheckout () { & git checkout $args }
function invoke-gitFetchOrig { git fetch origin }
Function invoke-GitLazy($path,$message) { cd $path ; git lazy $message } ;
Function invoke-GitLazySilently {Out-File -FilePath .\lazy.log -inputObject (invoke-GitLazy 'AutoCommit' 2>&1 )} ; #todo: parameterize #todo: rename to more descriptive #todo: breakout
function invoke-gitRemote { param ($subCommand = 'get-url',$name = "origin" ) git remote $subCommand $name }
Function invoke-GitSubmoduleAdd([string]$leaf,[string]$remote,[string]$branch) { git submodule add -f --name $leaf -- $remote $branch ; git commit -am $leaf+$remote+$branch } ; #todo: move to git aliases #Git Ad $leaf as submodule from $remote and branch $branch
}
if ( $null -ne $(Get-Module PSReadline -ea SilentlyContinue)) {
function find-historyAppendClipboard($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{ Set-Clipboard -Value $_ }} #search history of past expressions and adds to clipboard
function find-historyInvoke($searchstring) { $path = get-historyPath; menu @( get-content $path | where{ $_ -match $searchstring }) | %{Invoke-Expression $_ } } #search history of past expressions and invokes it, doesn't register the expression itself in history, but the pastDo expression.
}
function split-fileByLineNr { param( $pathName = '.\gron.csv',$OutputFilenamePattern = 'output_done_' , $LineLimit = 60) ;
$ext = $pathName | split-path -Extension
$inputx = Get-Content ;
$line = 0 ;
$i = 0 ;
$path = 0 ;
$start = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq $LineLimit -Or $line -eq $inputx.Length) {
$path++ ;
$pathname = "$OutputFilenamePattern$path$ext" ;
$inputx[$start..($line - 1)] | Out -File $pathname -Force ;
$start = $line ;
$i = 0 ;
Write-Host "$pathname" ;
} ;
$i++ ;
$line++
}
}
function split-fileByMatch($pathName , $regex) { #param( $pathName = 'C:\Users\crbk01\Documents\WindowsPowerShell\snipps\Modules\Todo SplitUp.psm1' , $regex = '(?<=function\s)[^\s\(]*') ;
$ext = ($pathName | split-path -Extension)
$parent = ($pathName | split-path -Parent)
$OriginalName = ($pathName | split-path -LeafBase)
$inputx = Get-Content $pathName; $line = 0 ; $i = 0 ; $start = @(select-string -path $pathName -pattern $regex ) | select linenumber ; $LineLimit = $start | select -Skip 1 ; $names = @() ; [regex]::matches($inputx,$regex).groups.value | %{$names+= $_ }
$occurence = 0 ;
while ($line -le $inputx.Length) {
if ($i -eq ([int]$LineLimit[$occurence].linenumber -1) -Or $line -eq $inputx.Length)
{
$currentName = $names[$occurence];
$pathname = Join-Path -path $parent -childPath "$OriginalName-$currentName$ext" ;
$u = ([int]$start[$occurence].linenumber -1)
$inputx[$u..($line - 1)] > $pathname
$occurence++ ;
Write-Host "$u..($line - 1)$pathname" ;
};
$i++ ;
$line++
}
}
#function aliasCode { & $env:code }
function .. { Set-Location ".." }
function .... { Set-Location (Join-Path -Path ".." -ChildPath "..") }
function all { process { $_ | Select-Object * } } # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#piping-objects-to-functions
function cdc { Set-Location "$XDG_CONFIG_HOME" }
function cdcheat { Set-Location "$CHEATS_DIR" }
function cdd { Set-Location "$DESKTOP_DIR" }
function cddd { Set-Location "$DEVEL_DIR" }
function cddev { Set-Location "$DEVEL_DIR" }
function cdn { Set-Location "$NOTES_DIR" }
function cdports { Set-Location "$PORTS_DIR" }
function cdt { Set-Location "$TODO_DIR" }
function clear-Days_Back { param( $path = "C:\Support\SQLBac\" ,$Daysback = "0" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item }
function ConvertFrom-Bytes { param( [string]$bytes, [string]$savepath ) $dir = Split-Path $savepath if (!(Test-Path $dir)) { md $dir | Out-Null } [convert]::FromBase64String($bytes) | Set-Content $savepath -Encoding Byte }
function ConvertTo-Bytes ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } [convert]::ToBase64String((Get-Content $path -Encoding Byte)) }
function df { get-volume }
function enter-dir { param( $path = '%USERPROFILE%\Desktop\' ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Set-Location $Path }; #if no param, navigate to desktop # 5. 更改工作目录 # 输入要切换到的路径 # 用法示例:cd C:/ # 默认路径:D 盘的桌面
function exit-Nrenter { shutdown /r } #reboot
function export($name, $value) { set-item -force -path "env:$name" -value $value; }
function foldercontain($folder,$name) { $q = get-childitem $folder; return $q -contains $name }
function Get-AllNic { Get-NetAdapter | Sort-Object -Property MacAddress } # 1. 获取所有 Network Interface
function get-Childnames { param( $path = $pwd) (Get-ChildItem -path $path).Name ; Write-Host("") }; # 3. 查看目录 ls & ll
function Get-DefaultAliases { Get-Alias | Where-Object { $_.Options -match "ReadOnly" }}
function get-envVar { Get-Childitem -Path Env:*}
function get-EspHeader { param( $path = '.\FormiD.esp', $max = 13,$headerSize = 3 ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $q = ((( -join (gc $path | select -first $headerSize)) -replace "[^\w. ] {1,}","å").split("å") -match "^.*[.](esp |esm)$") ; $q | ? {($q | select -first $max) -NotContains $_}}
function get-historyPath { (Get-PSReadlineOption).HistorySavePath }
function Get-IPv4Routes { Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'} } # 2. 获取 IPv4 关键路由
function Get-IPv6Routes { Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'} } # 3. 获取 IPv6 关键路由
function get-isFolder {$PSBoundParameters -is [system.in.folder]}
function get-parameters { Get-Member -Parameter *}
function get-RegInstallpaths { Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | % { Get -ItemProperty $_.PsPath } | Select DisplayName,InstallLocation }
function get-whatpulse { param( $program,$path) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $query = "select rightstr(path,instr(reverse(path),'/') -1) exe,path from (select max(path) path,max(cast(replace(version,'.','') as integer)) version from applications group by case when online_app_id = 0 then name else online_app_id end)" ; $adapter = newSqliteConnection -source (Everything 'whatpulse.db')[0] -query $query ; $b=@($data.item('exe')) ; $a = @($data.item('path')) ; $i=0 ; while($i -lt $a.Length) {$res[$b[$i]]=$a[$i] ; $i++ } ; $res | where { $_.name -match $program -and $_.path -match $path}}
function grep { process { $_ | Select-String -Pattern $args } } # function grep($regex, $dir) { if ( $dir ) { ls $dir | select-string $regex return } $input | select-string $regex }
function grepv($regex) { $input | ? { !$_.Contains($regex) } }
function Initialize-Profile { . $PROFILE.CurrentUserCurrentHost} #function initialize-profile { & $profile } #reload-profile is an unapproved verb.
function invoke-Nmake { nmake.exe $args -nologo }; # 1. 编译函数 make
function invoke-powershellAsAdmin { Start-Process powershell -Verb runAs } #new ps OpenAsADmin
function join-ByRuncunfig { param( $prefix='[$]APPLICATION_CONFIG_DIR[$][/]'
,$refixReplace='C:\Users\crbk01\AppData\Roaming\JetBrains\DataGrip2021.1\',
$runconfig="TillMinaMedelanden.run.xml",$output='runConfig/Combined.sql') [xml]$xml=get -content $runConfig -Encoding UTF8 ; $xml.component.configuration.'script -file' | %
{$_.value -replace $prefix, ($prefixReplace -replace '\\','/')} | % {" - -:$_"
; get -content -path $_ -Encoding UTF8 ; "go" } >> $output }
function list { process { $_ | Format-List * } } # fl is there by default
function man { Get-Help $args[0] | out-host -paging }
function md { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function measure-ExtOccurenseRecursivly { param( $path = "D:\Project Shelf\MapBasic" ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } Get-ChildItem -Path $path -Recurse -File | group Extension -NoElement | sort Count -Descending | select -Property name }
function measure-words { param( $inputStream = (Get -ChildItem | % { Get -Content $_.name }), $regex="[^a-z0-9_-]") $hash = @{} ; $a="" ; $inputStream | % {$a+=$_} ; $a -split $regex | % { $_.tolower()} | % {$hash[$_]++} ; $sorted= % {$hash.GetEnumerator() | sort -object {[int]$_.value}} ; return $sorted} ; $sorted | where{$_.name -notmatch "^\d+$"} | where{$_.name.length -gt 4 }
function mkdir { New-Item -type directory -path (Join-Path "$args" -ChildPath "") }
function My-Scripts { Get-Command -CommandType externalscript }
function open-here { param( $Path = $pwd ) Invoke-Item $Path }; # 4. 打开当前工作目录 # 输入要打开的路径 # 用法示例:open C:\ # 默认路径:当前工作文件夹
function open-ProfileFolder { explorer (split-path -path $profile -parent)}
function pgrep($name) { Get-Process $name }
function pkill($name) { Get-Process $name -ErrorAction SilentlyContinue | kill }
function pull () { & get pull $args }
function read-aliases { Get-Alias | Where-Object { $_.Options -notmatch "ReadOnly" }}
function read-EnvPaths { ($Env:Path).Split(";") }
function read-headOfFile { param( $linr = 10, $path ) if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } gc -Path $path -TotalCount $linr }
function read-json { param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PSCustomObject] $input ) $json = [ordered]@{}; ($input).PSObject.Properties | % { $json[$_.Name] = $_.Value } $json.SyncRoot }
function read-paramNaliases ($command) { (Get-Command $command).parameters.values | select name, @{n='aliases';e={$_.aliases}} }
function read-pathsAsStream { get-childitem | out-string -stream } # filesInFolAsStream ;
function read-uptime { Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime'; EXPRESSION= {$_.ConverttoDateTime($_.lastbootuptime)}} } #doesn't psreadline module implement this already?
function Remove-CustomAliases { Get-Alias | Where-Object { ! $_.Options -match "ReadOnly" } | % { Remove-Item alias:$_ }} # https://stackoverflow.com/a/2816523
function remove-TempfilesNfolders { foreach ($folder in get-tempfilesNfolders) {Remove-Item $folder -force -recurse} }
function Sanatize-path { param( $path='G:\mhk',$replaceChar='_') if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } $regex= "[^\)\w \\\ -:.åäöÅÄÖ %\ {,~$\(!@¤£é&=#§]" ; Get -ChildItem $path -Recurse | Where -Object {$_.name -match $regex} | rename -item -newName {$_.name -replace $regex,$replaceChar}}
function sed($path, $find, $replace) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } (Get-Content $path).replace("$find", $replace) | Set-Content $path }
function set-FileEncodingUtf8 ( [string]$path ) { if (!$path -or !(Test-Path $path)) { throw "file not found: '$path'" } sc $path -encoding utf8 -value(gc $path) }
function set-x { Set-PSDebug -trace 2}
function set+x { Set-PSDebug -trace 0}
function sort-PathByLvl { param( $inputList) $inputList | Sort {($_ -split '\\').Count}, {$_} -Descending | select -object -first 2 | % { $error.clear() ; try { out -null -input (test -ModuleManifest $_ > '&2>&1' ) } catch { "Error" } ; if (!$error) { $_ } }}
function start-BrowserFlags { vivaldi "vivaldi://flags" } #todo: use standard browser instead of hardcoded
function start-notepad-profile { notepad $profile}
function string { process { $_ | Out-String -Stream } }
function touch($path) { "" | Out-File $path -Encoding ASCII }
function which($name) { Get-Command $name | Select-Object -ExpandProperty Definition } #should use more
Remove-Item alias:ls -ea SilentlyContinue ; function ls { Get-Childitem} # ls -al is musclememory by now so ignore all args for this "alias"
#------------------------------- Functions END -------------------------------
#------------------------------- Set alias BEGIN -------------------------------
$TAType = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators") ; $TAType::Add('accelerators',$TAType)
set-alias accelerators [accelerators]::Get
set-alias edprofile start-Notepad-Profile
set-alias exp-pro open-ProfileFolder
set-alias kidStream get-childitem | out-string -stream
set-alias history (Get-PSReadlineOption).HistorySavePath
set-alias parameters get-parameters
set-alias start-su start-powershellAsAdmin
set-alias version $PSVersionTable # bash-like
Set-Alias cat Get-Content -Option AllScope
Set-Alias cd Set-Location -Option AllScope
Set-Alias clear Clear-Host -Option AllScope
Set-Alias cp Copy-Item -Option AllScope
Set-Alias history Get-History -Option AllScope
Set-Alias kill Stop-Process -Option AllScope
Set-Alias lp Out-Printer -Option AllScope
Set-Alias mv Move-Item -Option AllScope
Set-Alias ps Get-Process -Option AllScope
Set-Alias pwd Get-Location -Option AllScope
Set-Alias which Get-Command -Option AllScope
Set-Alias open Invoke-Item -Option AllScope
Set-Alias basename Split-Path -Option AllScope
Set-Alias realpath Resolve-Path -Option AllScope # cmd-like Set-Alias rm Remove-Item -Option AllScope
Set-Alias rmdir Remove-Item -Option AllScope
Set-Alias echo Write-Output -Option AllScope
Set-Alias cls Clear-Host -Option AllScope
Set-Alias chdir Set-Location -Option AllScope
Set-Alias copy Copy-Item -Option AllScope
Set-Alias del Remove-Item -Option AllScope
Set-Alias dir Get-Childitem -Option AllScope
Set-Alias erase Remove-Item -Option AllScope
Set-Alias move Move-Item -Option AllScope
Set-Alias rd Remove-Item -Option AllScope
Set-Alias ren Rename-Item -Option AllScope
Set-Alias set Set-Variable -Option AllScope
Set-Alias type Get-Content -Option AllScope
Set-Alias env Get-Environment -Option AllScope # custom aliases
Set-Alias flush-dns Clear-DnsClientCache -Option AllScope
Set-Alias touch Set-FileTime -Option AllScope
set-alias lsx get-Childnames -Option AllScope
set-alias filesinfolasstream read-childrenAsStream -Option AllScope
set-alias bcompare start-bc -Option AllScope
set-alias GitAdEPathAsSNB invoke-GitSubmoduleAdd -Option AllScope
set-alias GitUp invoke-GitLazy -Option AllScope
set-alias gitSilently invoke-GitLazySilently -Option AllScope
set-alias gitSingleRemote invoke-gitFetchOrig -Option AllScope
set-alias executeThis invoke-FuzzyWithEverything -Option AllScope
set-alias filesinfolasstream read-pathsAsStream -Option AllScope
set-alias everything invoke-Everything -Option AllScope
set-alias make invoke-Nmake -Option AllScope
set-alias MyAliases read-aliases -Option AllScope
set-alias OpenAsADmin invoke-powershellAsAdmin -Option AllScope
set-alias home open-here -Option AllScope
set-alias pastDo find-historyInvoke -Option AllScope
set-alias pastDoEdit find-historyAppendClipboard -Option AllScope
set-alias HistoryPath (Get-PSReadlineOption).HistorySavePath -Option AllScope
set-alias reboot exit-Nrenter -Option AllScope
set-alias browserflags start-BrowserFlags -Option AllScope
set-alias df get-volume -Option AllScope
set-alias printpaths read-EnvPaths -Option AllScope
set-alias reload initialize-profile -Option AllScope
set-alias uptime read-uptime -Option AllScope
set-alias getnic get-mac -Option AllScope # 1. 获取所有 Network Interface set-alias ll Get-ChildItem -Option AllScope
set-alias getip Get-IPv4Routes -Option AllScope
set-alias getip6 Get-IPv6Routes -Option AllScope
set-alias os-update Update-Packages -Option AllScope
set-alias remote invoke-gitRemote -Option AllScope
set-alias gitsplit subtree-split-rm-commit -Option AllScope
set-alias new-name Rename-Item -Option AllScope
set-alias set-name Rename-Item -Option AllScope
set-alias isFolder get-isFolder -Option AllScope
set-alias start-powershellAsAdmin invoke-powershellAsAdmin -Option AllScope
set-alias psVersion $PSVersionTable.PSVersion.Major -Option AllScope
#------------------------------- Set alias END -------------------------------
Write-Host "PSVersion: $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Patch)"
Write-Host "PSEdition: $($PSVersionTable.PSEdition)"
Write-Host "Profile: $PSCommandPath"
<#
* FileName: Microsoft.PowerShell_profile.ps1
* Author: perXautomatik
* Email: [email protected]
* Date: 08/03/2022
* Copyright: No copyright. You can use this code for anything with no warranty.
#>
#loadMessage
echo "Microsoft.PowerShell_profile.ps1"
# Increase history
$MaximumHistoryCount = 10000
#src: https://stackoverflow.com/a/34098997/7595318
function Test-IsInteractive {
# Test each Arg for match of abbreviated '-NonInteractive' command.
$NonInteractiveFlag = [Environment]::GetCommandLineArgs() | Where-Object{ $_ -like '-NonInteractive' }
if ( (-not [Environment]::UserInteractive) -or ( $NonInteractiveFlag -ne $null ) ) {
return $false
}
return $true
}
#if ( Test-IsInteractive ) { (preferably use -noLogo) } # Clear-Host # remove advertisements
function Download-Latest-Profile {
New-Item $( Split-Path $($PROFILE.CurrentUserCurrentHost) ) -ItemType Directory -ea 0
if ( $(Get-Content "$($PROFILE.CurrentUserCurrentHost)" | Select-String "62a71500a0f044477698da71634ab87b" | Out-String) -eq "" ) {
Move-Item -Path "$($PROFILE.CurrentUserCurrentHost)" -Destination "$($PROFILE.CurrentUserCurrentHost).bak"
}
Invoke-WebRequest -Uri "https://gist.githubusercontent.com/apfelchips/62a71500a0f044477698da71634ab87b/raw/Profile.ps1" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
Reload-Profile
}
# Produce UTF-8 by default
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8' # Fix Encoding for PS 5.1 https://stackoverflow.com/a/40098904
}
#------------------------------- Set Paths -------------------------------
$paths = join-path -Path (split-path $profile -Parent) -ChildPath 'setPaths.ps1'
Import-Module $paths
#------------------------------- Set Paths end -------------------------------
#------------------------------- Import Modules BEGIN -------------------------------
$profileFolder = (split-path $profile -Parent)
$pos = join-path -Path $profileFolder -ChildPath 'importModules.ps1'
Import-Module $pos
#------------------------------- Import Modules END -------------------------------
#------------------------------- overloading begin
#https://www.sapien.com/blog/2014/10/21/a-better-tostring-method-for-hash-tables/
#better hashtable ToString method
Update-TypeData -TypeName "System.Collections.HashTable" `
-MemberType ScriptMethod `
-MemberName "ToString" -Value { $hashstr = "@{"; $keys = $this.keys; foreach ($key in $keys) { $v = $this[$key];
if ($key -match "\s") { $hashstr += "`"$key`"" + "=" + "`"$v`"" + ";" }
else { $hashstr += $key + "=" + "`"$v`"" + ";" } }; $hashstr += "}";
return $hashstr }
#------------------------------- overloading end
#------------------------------- SystemMigration -------------------------------
#choco check if installed
#path to list of aps to install
#choco ask to install if not present
#list of portable apps,download source
#path
#download and extract if not present, ask to confirm
#path to portable apps
#path to standard download location
#git Repos paths and origions,
#git systemwide profile folder
#git global path
#everything data folder
#autohotkey script to run on startup
#startup programs
#reg to add if not present
#------------------------------- SystemMigration end -------------------------------
#------------------------------- Styling begin --------------------------------------
#change selection to neongreen
#https://stackoverflow.com/questions/44758698/change-powershell-psreadline-menucomplete-functions-colors
$colors = @{
"Selection" = "$([char]0x1b)[38;2;0;0;0;48;2;178;255;102m"
}
Set-PSReadLineOption -Colors $colors
# Style default PowerShell Console
$shell = $Host.UI.RawUI
$shell.WindowTitle= "PS"
$shell.BackgroundColor = "Black"
$shell.ForegroundColor = "White"
# Load custom theme for Windows Terminal
#Set-Theme LazyAdmin
#------------------------------- Credit to : apfelchips -------------------------------
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
function Install-PowerShellGet { Start-Process "$(Get-HostExecutable)" -ArgumentList "-noProfile -noLogo -Command Install-PackageProvider -Name NuGet -Force; Install-Module -Name PowerShellGet -Repository PSGallery -Force -AllowClobber -SkipPublisherCheck; pause" -verb "RunAs"}
}
function Test-ModuleExists {
#retuns module version if exsists else false
Param ($name)
$x = Get-Module -ListAvailable -Name $name
return $x ?? $false
}
#src: https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
function Test-CommandExists {
Param ($command)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { Get-Command $command; return $true }
catch {return $false}
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Get-ModulesAvailable {
if ( $args.Count -eq 0 ) {
Get-Module -ListAvailable
} else {
Get-Module -ListAvailable $args
}
}
function Get-ModulesLoaded {
if ( $args.Count -eq 0 ) {
Get-Module -All
} else {
Get-Module -All $args
}
}
function TryImport-Module {
param($name)
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$errorPath = join-path -Path (split-path $profile -Parent) -ChildPath "$name.error.load.log"
try { Import-Module $name && echo "i $name"}
catch { "er.loading $name" ; $error > $errorPath }
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Tryinstall-Module {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$errorPath = join-path -Path (split-path $profile -Parent) -ChildPath "$name.error.install.log"
try {
if ( $args.Count -eq 1 ) {
Invoke-Expression "PowerShellGet\Install-Module -Name $args[1] -Scope CurrentUser -Force -AllowClobber"
}
elseif ( $args.Count -eq 2 ) {
Invoke-Expression "PowerShellGet\Install-Module -Name $args[1] -Scope CurrentUser -Force -AllowClobber $args[2]"
}
elseif ($args.count -ne 0)
{
Invoke-Expression "PowerShellGet\Install-Module $args"
}
echo "i $name"
}
catch { "er.installing $name" ; $error > $errorPath }
finally { $ErrorActionPreference=$oldErrorActionPreference }
}
function Install-MyModules {
Tryinstall-Module 'PSReadLine' -AllowPrerelease
Tryinstall-Module 'posh-git'
Tryinstall-Module 'PSFzf'
Tryinstall-Module 'PSEverything'
Tryinstall-Module 'PSProfiler' # --> Measure-Script
# serialization tools: eg. ConvertTo-HashString / ConvertTo-HashTable https://github.com/torgro/HashData
Tryinstall-Module 'hashdata'
# useful Tools eg. ConvertTo-FlatObject, Join-Object... https://github.com/RamblingCookieMonster/PowerShell
Tryinstall-Module 'WFTools'
# https://old.reddit.com/r/AZURE/comments/fh0ycv/azuread_vs_azurerm_vs_az/
# https://docs.microsoft.com/en-us/microsoft-365/enterprise/connect-to-microsoft-365-powershell
Tryinstall-Module 'AzureAD'
Tryinstall-Module 'Pscx'
Tryinstall-Module 'SqlServer'
if ( $IsWindows ){
# Windows Update CLI tool http://woshub.com/pswindowsupdate-module/#h2_2
# Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
# native alternative: WindowsUpdateProvider\Install-WUUpdates >= Windows Server 2019
Tryinstall-Module 'PSWindowsUpdate'
}
}
Import-Module -Name (join-path -Path (split-path $profile -Parent) -ChildPath "sqlite.ps1")
function Import-MyModules {
if (!( ""-eq "${env:ChocolateyInstall}" )) {
TryImport-Module "${env:ChocolateyInstall}\helpers\chocolateyProfile.psm1"
}
# does not load but test if avialable to speed up load time
# ForEach-Object { TryImport-Module -name $_ } #-parralel for ps 7 does not work currently
$modules = @( 'PowerShellGet', 'PSProfiler', 'hashdata','WFTools','AzureAD','SqlServer','PSWindowsUpdate','echoargs','pscx' )
$modules | ForEach-Object { $null = Test-ModuleExists $_ || "error $_" }
# 引入 posh-git
if ( ($host.Name -eq 'ConsoleHost') -and ($null -ne (Get-Module -ListAvailable -Name posh-git)) )
{ TryImport-Module posh-git }
# 引入 oh-my-posh
TryImport-Module oh-my-posh
if ( (Test-ModuleExists 'oh-my-posh' )) {
Set-PoshPrompt ys
Set-PoshPrompt paradox
}
# 设置 PowerShell 主题
# 引入 ps-read-line # useful history related actions
# example: https://github.com/PowerShell/PSReadLine/blob/master/PSReadLine/SamplePSReadLineProfile.ps1
if ( ($host.Name -eq 'ConsoleHost') -and (Test-ModuleExists 'PSReadLine' )) {
#TryImport-Module PSReadLine
#------------------------------- Set Hot-keys BEGIN -------------------------------
$PSReadLineOptions = @{
PredictionSource = "HistoryAndPlugin"
HistorySearchCursorMovesToEnd = $true
}
Set-PSReadLineOption @PSReadLineOptions
# Set-PSReadLineOption -EditMode Emac
# 每次回溯输入历史,光标定位于输入内容末尾
# 设置 Tab 为菜单补全和 Intellisense
# 设置 Ctrl+d 为退出 PowerShell
# 设置 Ctrl+z 为撤销
# 设置向上键为后向搜索历史记录 # Autocompletion for arrow keys @ https://dev.to/ofhouse/add-a-bash-like-autocomplete-to-your-powershell-4257
Set-PSReadlineKeyHandler -Chord 'Shift+Tab' -Function Complete
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#------------------------------- Set Hot-keys END -------------------------------
if ( $(Get-Module PSReadline).Version -ge 2.2 ) {
# 设置预测文本来源为历史记录
Set-PSReadLineOption -predictionsource history -ea SilentlyContinue
}
if ( $null -ne $(Get-Module PSFzf) ) {
#Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }
#$FZF_COMPLETION_TRIGGER='...'
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
}
}
}
Import-MyModules; echo "modules imported"
#ps setHistorySavePath
if (-not $env:XDG_CONFIG_HOME) { $env:XDG_CONFIG_HOME = Join-Path -Path "$HOME" -ChildPath ".config" }; $XDG_CONFIG_HOME = $env:XDG_CONFIG_HOME
if (-not $env:XDG_DATA_HOME) { $env:XDG_DATA_HOME = Join-Path -Path "$HOME" -ChildPath ".local/share" }; $XDG_DATA_HOME = $env:XDG_DATA_HOME
if (-not $env:XDG_CACHE_HOME) { $env:XDG_CACHE_HOME = Join-Path -Path "$HOME" -ChildPath ".cache" }; $XDG_CACHE_HOME = $env:XDG_CACHE_HOME
if (-not $env:DESKTOP_DIR) { $env:DESKTOP_DIR = Join-Path -Path "$HOME" -ChildPath "desktop" }; $DESKTOP_DIR = $env:DESKTOP_DIR
if (-not $env:NOTES_DIR) { $env:NOTES_DIR = Join-Path -Path "$HOME" -ChildPath "notes" }; $NOTES_DIR = $env:NOTES_DIR
if (-not $env:CHEATS_DIR) { $env:CHEATS_DIR = Join-Path -Path "$env:NOTES_DIR" -ChildPath "cheatsheets" }; $CHEATS_DIR = $env:CHEATS_DIR
if (-not $env:TODO_DIR) { $env:TODO_DIR = Join-Path -Path "$env:NOTES_DIR" -ChildPath "_ToDo" }; $TODO_DIR = $env:TODO_DIR
if (-not $env:DEVEL_DIR) { $env:DEVEL_DIR = Join-Path -Path "$HOME" -ChildPath "devel" }; $DEVEL_DIR = $env:DEVEL_DIR
if (-not $env:PORTS_DIR) { $env:PORTS_DIR = Join-Path -Path "$HOME" -ChildPath "ports" }; $PORTS_DIR = $env:PORTS_DIR
# Load scripts from the following locations
$profileFolder = (split-path $profile -Parent)
$EnvPath = join-path -Path $profileFolder -ChildPath 'Snipps'
$env:Path += ";$EnvPath"
$historyPath = "$home\appdata\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt"
set-PSReadlineOption -HistorySavePath $historyPath
echo "historyPath: $historyPath"
#$path = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
# vscode Portable Path
$vscodepath = 'D:\portapps\6, Text,programming, x Editing\PortableApps\vscode-portable\vscode-portable.exe'
[Environment]::SetEnvironmentVariable("code", $vscodepath)
#sqlite dll
$workpath = "C:\Program Files\System.Data.SQLite\2010\bin\System.Data.SQLite.dll" ;
$alternative = (everything 'wfn:System.Data.SQLite.DLL')[0] ;
$p = if(Test-Path $workpath){$workpath} else {$alternative} ;
Add-Type -Path $p
echo $p
### local variables
$whatPulseDbQuery = "select rightstr(path,instr(reverse(path),'/')-1) exe,path from (select max(path) path,max(cast(replace(version,'.','') as integer)) version from applications group by case when online_app_id = 0 then name else online_app_id end)"
$whatPulseDbPath = (Everything 'whatpulse.db')[0];
[Environment]::SetEnvironmentVariable("WHATPULSE_DB", $whatPulseDbPath)
if (-not $env:WHATPULSE_DB) { $env:WHATPULSE_DB = $whatPulseDbPath }; $WHATPULSE_DB = $env:WHATPULSE_DB
[Environment]::SetEnvironmentVariable("WHATPULSE_QUERY", $whatPulseDbQuery)
if (-not $env:WHATPULSE_QUERY) { $env:WHATPULSE_QUERY = $whatPulseDbQuery }; $WHATPULSE_QUERY = $env:WHATPULSE_QUERY
$datagripPath = '$home\appdata\Roaming\JetBrains\DataGrip2021.1'
[Environment]::SetEnvironmentVariable("datagripPath", $datagripPath)
$bComparePath = 'D:\PortableApps\2. fileOrganization\PortableApps\Beyond Compare 4'
[Environment]::SetEnvironmentVariable("bComparePath", $bComparePath)
echo "paths set"
function NewSqliteConnection {
param(
[Parameter(Mandatory=$true)]
[string]$source
,[Parameter(Mandatory=$true)]
[string]$query
)
$con = New-Object -TypeName System.Data.SQLite.SQLiteConnection
;$safePath = $source.trim()
$con.ConnectionString = "Data Source=$safepath"
;
$con.Open() ;
$sql = $con.CreateCommand() ;
$sql.CommandText = "$QUERY" ;
$adapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter $sql ;
$data = New-Object System.Data.DataTable ;
$adapter.fill($data) ;
$adapter
}
<#
* FileName: Microsoft.PowerShell_profile.ps1
* Author: perXautomatik
* Email: [email protected]
* Date: 08/03/2022
* Copyright: No copyright. You can use this code for anything with no warranty.
#>
#loadMessage
echo "Microsoft.PowerShell_profile.ps1"
# Increase history
$MaximumHistoryCount = 10000
#src: https://stackoverflow.com/a/34098997/7595318
function Test-IsInteractive {
# Test each Arg for match of abbreviated '-NonInteractive' command.
$NonInteractiveFlag = [Environment]::GetCommandLineArgs() | Where-Object{ $_ -like '-NonInteractive' }
if ( (-not [Environment]::UserInteractive) -or ( $NonInteractiveFlag -ne $null ) ) {
return $false
}
return $true
}
#if ( Test-IsInteractive ) { (preferably use -noLogo) } # Clear-Host # remove advertisements
function Download-Latest-Profile {
New-Item $( Split-Path $($PROFILE.CurrentUserCurrentHost) ) -ItemType Directory -ea 0
if ( $(Get-Content "$($PROFILE.CurrentUserCurrentHost)" | Select-String "62a71500a0f044477698da71634ab87b" | Out-String) -eq "" ) {
Move-Item -Path "$($PROFILE.CurrentUserCurrentHost)" -Destination "$($PROFILE.CurrentUserCurrentHost).bak"
}
Invoke-WebRequest -Uri "https://gist.githubusercontent.com/apfelchips/62a71500a0f044477698da71634ab87b/raw/Profile.ps1" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
Reload-Profile
}
# Produce UTF-8 by default
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8' # Fix Encoding for PS 5.1 https://stackoverflow.com/a/40098904
}
#------------------------------- Set Paths -------------------------------
$paths = join-path -Path (split-path $profile -Parent) -ChildPath 'setPaths.ps1'
Import-Module $paths
#------------------------------- Set Paths end -------------------------------
#------------------------------- Import Modules BEGIN -------------------------------
$profileFolder = (split-path $profile -Parent)
$pos = join-path -Path $profileFolder -ChildPath 'importModules.ps1'
Import-Module $pos
#------------------------------- Import Modules END -------------------------------
#------------------------------- overloading begin
#https://www.sapien.com/blog/2014/10/21/a-better-tostring-method-for-hash-tables/
#better hashtable ToString method
Update-TypeData -TypeName "System.Collections.HashTable" `
-MemberType ScriptMethod `
-MemberName "ToString" -Value { $hashstr = "@{"; $keys = $this.keys; foreach ($key in $keys) { $v = $this[$key];
if ($key -match "\s") { $hashstr += "`"$key`"" + "=" + "`"$v`"" + ";" }
else { $hashstr += $key + "=" + "`"$v`"" + ";" } }; $hashstr += "}";
return $hashstr }
#------------------------------- overloading end
#------------------------------- SystemMigration -------------------------------
#choco check if installed
#path to list of aps to install
#choco ask to install if not present
#list of portable apps,download source
#path
#download and extract if not present, ask to confirm
#path to portable apps
#path to standard download location
#git Repos paths and origions,
#git systemwide profile folder
#git global path
#everything data folder
#autohotkey script to run on startup
#startup programs
#reg to add if not present
#------------------------------- SystemMigration end -------------------------------
#------------------------------- Styling begin --------------------------------------
#change selection to neongreen
#https://stackoverflow.com/questions/44758698/change-powershell-psreadline-menucomplete-functions-colors
$colors = @{
"Selection" = "$([char]0x1b)[38;2;0;0;0;48;2;178;255;102m"
}
Set-PSReadLineOption -Colors $colors
# Style default PowerShell Console
$shell = $Host.UI.RawUI
$shell.WindowTitle= "PS"
$shell.BackgroundColor = "Black"
$shell.ForegroundColor = "White"
# Load custom theme for Windows Terminal
#Set-Theme LazyAdmin
<#
* FileName: Microsoft.PowerShell_profile.ps1
* Author: perXautomatik
* Email: [email protected]
* Date: 08/03/2022
* Copyright: No copyright. You can use this code for anything with no warranty.
#>
#loadMessage
echo "Microsoft.PowerShell_profile.ps1"
# Increase history
$MaximumHistoryCount = 10000
#src: https://stackoverflow.com/a/34098997/7595318
function Test-IsInteractive {
# Test each Arg for match of abbreviated '-NonInteractive' command.
$NonInteractiveFlag = [Environment]::GetCommandLineArgs() | Where-Object{ $_ -like '-NonInteractive' }
if ( (-not [Environment]::UserInteractive) -or ( $NonInteractiveFlag -ne $null ) ) {
return $false
}
return $true
}
#if ( Test-IsInteractive ) { (preferably use -noLogo) } # Clear-Host # remove advertisements
function Download-Latest-Profile {
New-Item $( Split-Path $($PROFILE.CurrentUserCurrentHost) ) -ItemType Directory -ea 0
if ( $(Get-Content "$($PROFILE.CurrentUserCurrentHost)" | Select-String "62a71500a0f044477698da71634ab87b" | Out-String) -eq "" ) {
Move-Item -Path "$($PROFILE.CurrentUserCurrentHost)" -Destination "$($PROFILE.CurrentUserCurrentHost).bak"
}
Invoke-WebRequest -Uri "https://gist.githubusercontent.com/apfelchips/62a71500a0f044477698da71634ab87b/raw/Profile.ps1" -OutFile "$($PROFILE.CurrentUserCurrentHost)"
Reload-Profile
}
# Produce UTF-8 by default
if ( $PSVersionTable.PSVersion.Major -lt 7 ) {
# https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8' # Fix Encoding for PS 5.1 https://stackoverflow.com/a/40098904
}
#------------------------------- Set Paths -------------------------------
$paths = join-path -Path (split-path $profile -Parent) -ChildPath 'setPaths.ps1'
Import-Module $paths
#------------------------------- Set Paths end -------------------------------
#------------------------------- Import Modules BEGIN -------------------------------
$profileFolder = (split-path $profile -Parent)
$pos = join-path -Path $profileFolder -ChildPath 'importModules.ps1'
Import-Module $pos
#------------------------------- Import Modules END -------------------------------
#------------------------------- overloading begin
#https://www.sapien.com/blog/2014/10/21/a-better-tostring-method-for-hash-tables/
#better hashtable ToString method
Update-TypeData -TypeName "System.Collections.HashTable" `
-MemberType ScriptMethod `
-MemberName "ToString" -Value { $hashstr = "@{"; $keys = $this.keys; foreach ($key in $keys) { $v = $this[$key];
if ($key -match "\s") { $hashstr += "`"$key`"" + "=" + "`"$v`"" + ";" }
else { $hashstr += $key + "=" + "`"$v`"" + ";" } }; $hashstr += "}";
return $hashstr }
#------------------------------- overloading end
#------------------------------- SystemMigration -------------------------------
#choco check if installed
#path to list of aps to install
#choco ask to install if not present
#list of portable apps,download source
#path
#download and extract if not present, ask to confirm
#path to portable apps
#path to standard download location
#git Repos paths and origions,
#git systemwide profile folder
#git global path
#everything data folder
#autohotkey script to run on startup
#startup programs
#reg to add if not present
#------------------------------- SystemMigration end -------------------------------
#------------------------------- Styling begin --------------------------------------
#change selection to neongreen
#https://stackoverflow.com/questions/44758698/change-powershell-psreadline-menucomplete-functions-colors
$colors = @{
"Selection" = "$([char]0x1b)[38;2;0;0;0;48;2;178;255;102m"
}
Set-PSReadLineOption -Colors $colors
# Style default PowerShell Console
$shell = $Host.UI.RawUI
$shell.WindowTitle= "PS"
$shell.BackgroundColor = "Black"
$shell.ForegroundColor = "White"
# Load custom theme for Windows Terminal
#Set-Theme LazyAdmin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment