How to remove dot sourced script in PowerShell?

If I have a source point:

. "\foo-bar.ps1" 

How can I get a list of all dotted scripts and how to remove "foo-bar / ps1" from dotted scripts?

+6
source share
3 answers

As far as I know, you cannot delete the point named script. This is why the modules introduced in PowerShell 2.0. See About_Modules

You can convert your "foo-bar.ps1" to a module. A module can be imported (Import-Module) and removed (Remove-Module).

+11
source

I agree with @JPBlanc that you cannot remove the dot-sourced script as a whole, but depending on your own coding conventions, you can do this in practice.

First, a few notes about why you cannot do this at all :

(1) PowerShell does not have the concept of a dot-sourced script as a separate object. Once you place it with a point source, its contents will become part of your current context, just as if you manually typed each line in the prompt. Thus, you cannot delete it because it does not actually exist :-)

(2) For the same reason as (1), you cannot list scenarios with an access point.

Now how to do it:

Depending on the contents and conventions of your script access point, you can do what you want. If, for example, the script simply defines three functions - call these func-a , func-b and func-c - then you can either list or delete strong> those functions.

List of Assimilated Functions:

 Get-ChildItem function:func-* 

Remove Assimilated Functions:

 Remove-Item function:func-* 
+5
source

You can remove the original functions if you know the path to the file with the access point:

 Get-ChildItem function: | Where-Object {$_.ScriptBlock.File -eq $path} | Remove-Item 
+3
source

All Articles