[PS] Remove-item ... I must be missing something

Discussion in 'Scripting' started by Andrew Watt [MVP], May 11, 2006.

  1. After the recent post about aliases I started playing with aliases and
    ways of removing them.

    One variant, which successfully removes all aliases if run from the
    command line is:
    remove-item -Path Alias:\* -Include * -force

    So far, so good.

    If I place the code in a tiny script, clear-alias.ps1 and run the
    script nothing is removed. I know the script has run because I
    included debug write-host cmdlets in it after it didn't work as
    expected.

    So, what am I missing here? Having had one "doh!" moment today, this
    could well be the second. :)

    Thanks.

    Andrew Watt
     
    Andrew Watt [MVP], May 11, 2006
    #1
    1. Advertisements

  2. Because you are removing the aliases - but ONLY in the script scope. :)

    There may be a way to explicitly globalize execution within the script, but
    the simplest technique is dot-sourcing. See below:

    PS> (gal).length
    101
    PS> clear-alias
    PS> (gal).length
    101
    PS> . clear-alias
    PS> (gal).length
    'gal' is not recognized as a cmdlet, function, operable program, or script
    file
    ..
    At line:1 char:5
    + (gal) <<<< .length
    PS>
     
    Alex K. Angelopoulos [MVP], May 11, 2006
    #2
    1. Advertisements

  3. When I execute

    .. clear-alias

    I get the error message "'clear-alias' is not recognized as a cmdlet,
    function, operable program, or script file.
    At line:1 char:2
    + . <<<< clear-alias"

    Did it work for you?

    But
    .. .\clear-alias
    does work.

    Thanks for the pointer.

    Andrew Watt MVP
     
    Andrew Watt [MVP], May 11, 2006
    #3
  4. It works fine for me, but that's because I save clear-alias into a folder in
    my search path.
     
    Alex K. Angelopoulos [MVP], May 11, 2006
    #4
  5. Andrew Watt [MVP]

    dreeschkind Guest

    This might be a little confusing, running remove-item on the filesystem
    provider within a script removes items NOT ONLY in the script scope... :(
     
    dreeschkind, May 11, 2006
    #5
  6. But
    Try

    .. ..\clear-alias

    -bruce

    --
    Bruce Payette [MSFT]
    Windows PowerShell Technical Lead
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Bruce Payette [MSFT], May 12, 2006
    #6
  7. Andrew Watt [MVP]

    dreeschkind Guest

    Yeah, it was my intention to point this out, because until now I had not
    read/thought about this particular feature at all.
    I think it shoud be mentioned in the help/documentation/books about
    PowerShell scoping/providers that there might be differences in the scoping
    support of possible providers.
     
    dreeschkind, May 12, 2006
    #7
  8. +1

    Andrew Watt MVP
     
    Andrew Watt [MVP], May 12, 2006
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.