diff --git a/Get-MyAliases.ps1 b/Get-MyAliases.ps1 new file mode 100644 index 0000000..a836f38 --- /dev/null +++ b/Get-MyAliases.ps1 @@ -0,0 +1,25 @@ +[CmdletBinding(SupportsShouldProcess)]param() + +$local:allAliases = @() + +$MyAliasScope = 0 +$local:_scope = 0 +$local:_done = $false +do { + try { + $local:newAliases += Get-Alias -Scope $_scope | + Where-Object { + ($_.Definition -match "^$MyPSScrtipRoot") -or ($_.Description -match '#MyAlias') + } + if( $newAliases ) { + $allAliases += $newAliases + $MyAliasScope = $_scope; + Write-Verbose "`$MyAliasScope is now set to $MyAliasScope" + } + $_scope += 1 + } catch { + $done = $_.Exception.Message -match 'The scope .* exceeds' + } +} until ( $done ) + +$allAliases diff --git a/README.md b/README.md index 656d0a8..c53536f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,37 @@ -# PowerShell_Scripts +# PowerShell Scripts -These are the scripts that go into $MyPSScriptRoot which is part of my $env:PATH. - -The development of these is documented in my blog: https://blog.lksz.me - -They are provided 'as is' for your review and for your reuse. \ No newline at end of file +These are the scripts that go into $MyPSScriptRoot which is part of my $env:PATH. + +The development of these is documented in my blog: https://blog.lksz.me + +They are provided 'as is' for your review and for your reuse. + +There is a [blog post](https://blog.lksz.me/keeping-track-of-everything/#-mypsscriptroot) about how to clone this repo and use it as your own. + +The scripts all [assume](https://blog.lksz.me/functions-or-scripts/#myprofilecode) the following code is part of one of your $PROFILE files: +``` +$global:PathEnvDelimiter = $(if( $PSVersionTable.Platform -match 'unix' ) {':'} else {';'}) +function Split-PathEnv { +param([string]$EnvPath) + $EnvPath -split $PathEnvDelimiter +} + +$global:MyPSModulePath = Split-PathEnv $env:PSModulePath | Where-Object { $_ -match "^$(Resolve-Path ~)" } + +if( -not $MyPSModulePath ) { + $MyPSModulePath = Resolve-Path ~/powershell/Modules + if( -not (Test-Path $MyPSModulePath) ) { + New-Item -ItemType Directory -Path $MyPSModulePath -Force | Out-Null + } + $env:PSModulePath = "$MyPSModulePath$PathEnvDelimiter$env:PSModulePath" +} + +$local:p = Split-PathEnv $env:PATH +$global:MyPSScrtipRoot = Join-Path (Split-Path -Parent $MyPSModulePath) Scripts +if( -not (Test-Path $MyPSScrtipRoot) ) { + New-Item -ItemType Directory -Path $MyPSScrtipRoot -Force | Out-Null +} + +$p = @($p[0], $MyPSScrtipRoot) + $($p | Select-Object -Skip 1) +$env:PATH = $p -join $PathEnvDelimiter +``` diff --git a/Reload-MyAliases.ps1 b/Reload-MyAliases.ps1 new file mode 100644 index 0000000..e1015b0 --- /dev/null +++ b/Reload-MyAliases.ps1 @@ -0,0 +1,32 @@ +[CmdletBinding(SupportsShouldProcess)]param() + +$local:myAliases = [ordered]@{} +$myAliases.sudo = 'Invoke-MySudo' +$myAliases.vi = 'Edit-TextFile' +$myAliases.vim = 'Edit-TextFile' +$myAliases.nvim = 'Edit-TextFile' +$myAliases.nvim = 'Edit-TextFile' + +####################################################################### + +$local:IsVerbose = [bool]($PSBoundParameters['Verbose']) + +$local:MyAliasScope = 1 + +$local:oldAliases = Get-MyAliases + +$oldAliases = Get-Alias -Scope $MyAliasScope | + Where-Object Name -in $($oldAliases.Name + $myAliases.Keys) + +if( $oldAliases -and $IsVerbose ) { + Write-Verbose "Removing: $($oldAliases.Name -join ', ')" +} +$oldAliases | Remove-Alias -Scope $MyAliasScope +Get-ChildItem (Join-Path $MyPSScrtipRoot '*.ps1') | + ForEach-Object { + Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope + } + +foreach( $local:alias in $myAliases.Keys ) { + Set-Alias -Name $alias -Value $myAliases[$alias] -Description '#MyAlias' -Scope $MyAliasScope +}