Script directory for a PowerShell profile. https://blog.lksz.me/keeping-track-of-everything/#-mypsscriptroot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.9 KiB

  1. [CmdletBinding(SupportsShouldProcess)]param()
  2. try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { Remove-Item "Alias:$($_.Name)" } } catch {}
  3. function Get-MyAliases {
  4. [CmdletBinding(SupportsShouldProcess)]param([switch]$ScriptsOnly)
  5. $local:allAliases = @()
  6. $MyAliasScope = 0
  7. $local:_scope = 0
  8. $local:_done = $false
  9. do {
  10. try {
  11. $local:newAliases += Get-Alias -Scope $_scope |
  12. Where-Object {
  13. ($_.Definition -match "^$MyPSScriptRoot") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias'))
  14. }
  15. if( $newAliases ) {
  16. $allAliases += $newAliases
  17. $MyAliasScope = $_scope;
  18. Write-Verbose "`$MyAliasScope is now set to $MyAliasScope"
  19. }
  20. } catch {
  21. Write-Verbose "catch: $($_.Exception.Message)"
  22. $_done = $_.Exception.Message -match 'The scope .* exceeds'
  23. }
  24. $_scope += 1
  25. } until ( $_done )
  26. $allAliases
  27. }
  28. function getScriptName{param([string]$FullPath)
  29. $FullPath -replace '\.ps1$','' -replace "^$([regex]::Escape($MyPSScriptRoot)).",''
  30. }
  31. $local:myAliases = [ordered]@{}
  32. if( Test-Path $(Join-Path $MyPSScriptRoot Aliases) ) {
  33. Get-ChildItem $(Join-Path $MyPSScriptRoot Aliases) | ForEach-Object {
  34. $myAliases[$_.BaseName] = Get-Content $_.FullName
  35. }
  36. }
  37. $local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
  38. $script:MyAliasScope = 0
  39. $local:oldAliases = Get-MyAliases
  40. $oldAliases = Get-Alias -Scope $MyAliasScope |
  41. Where-Object Name -in $($oldAliases.Name + $myAliases.Keys)
  42. if( $oldAliases -and $IsVerbose ) {
  43. Write-Verbose "Removing: $($oldAliases.Name -join ', ')"
  44. }
  45. if( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) {
  46. $oldAliases | Remove-Alias -Scope $MyAliasScope
  47. } else {
  48. $oldAliases | ForEach-Object { Remove-Item "Alias:$($_.Name)" }
  49. }
  50. if( $(. Get-ScopeDepth) -gt 0 ) { Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it" }
  51. Get-ChildItem $(Join-Path $MyPSScriptRoot profile.d) -Filter '*.ps1' | ForEach-Object {
  52. Write-Verbose "Loading $(getScriptName $_.FullName)...";
  53. ". '$($_.FullName)';"
  54. } | Invoke-Expression
  55. $local:CommandsToAlias = (
  56. @( $MyPSScriptRoot ) + $(
  57. [SystemName]::_GetValidValues("",$true,$true) | ForEach-Object {
  58. Join-Path $MyPSScriptRoot "sys.$_"
  59. }
  60. )) | ForEach-Object {
  61. if( Test-Path $_ ) {
  62. Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$'
  63. }
  64. }
  65. $CommandsToAlias | ForEach-Object {
  66. Write-Verbose "Creating alias for $(getScriptName $_.FullName) Script..."
  67. Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope
  68. }
  69. foreach( $local:alias in $myAliases.Keys ) {
  70. Write-Verbose "Adding $($alias) alias..."
  71. Set-Alias -Name $alias -Value $myAliases[$alias] -Description '#MyAlias' -Scope $MyAliasScope
  72. }