#How to delete alias with AllScope option from module

11 messages ยท Page 1 of 1 (latest)

cyan wave
#

I manage my profile as a module, for some reason I'd like to remove default md alias, and use my md function instead:

# in a module file
if (Test-Path alias:md) {
    Remove-Alias md -Scope Global
}
# my md impl
function md {
    $null = New-Item -ItemType Directory @args
}

But it didn't work, I still have md alias in session, it seems aliases with option AllScope can't be deleted from within a module? I can confirm that alias with option None can be deleted successfully

EDIT: add example to reproduce the problem

New-Module { Remove-Item alias:md }
Get-Command md # md still exists

New-Module { Remove-Item alias:wjb }
Get-Command wjb # would error, wjb was removed as expected
oblique cove
#

did you try Remove-Alias md -Scope Global -Force

cyan wave
gusty ingot
# cyan wave yes, didn't work unfortunately

This part works in a session. You might want to alter it for a module

function md.nin { "๐Ÿ’" }
Set-Alias 'md' -Value md.nin -PassThru -Force -Option AllScope

md
gcm 'md', 'md.*'  
#

For a module you probably want -Scope Script. I'm not certain on which Option
I think the trick was replacing, not removing it

cyan wave
gusty ingot
#

Oh wait, are you asking to make code from a module

  • modify the user's scope, ie global or
  • modify the module's scope, ie: script
cyan wave
gusty ingot
# cyan wave not sure what you mean, I import the module in global scope. The problem is real...

This visual might help. First I saved this file as ModuleSetUserAlias.psm1

import-module pansies

function md.nin { "๐Ÿ’" }
Set-Alias 'md' -Value md.nin -PassThru -Force -Option AllScope
    | Write-Host -fg 'orange' -bg 'gray20'

Then Imported it

$modPath = gi .\ModuleSetUserAlias.psm1
ipmo $modPath -Force -PassThru -DisableNameChecking
# [1] this fails
md 

# [2] this works
& ( ipmo $Modpath -PassThru ) { md }
#

[1] is the users scope
[2] is showing that it worked in the module's scope

#

Try this. I believe it's the global scope you are asking for

# Your.psm1
function md2 { "๐Ÿˆ" }
Set-Alias 'md' -Value md2 -PassThru -Force -Option AllScope -Scope Global