Suppose you're a system admin who uses powershell to managa a lot of things on his/her system(s).
You've probably written a lot of functions which do things you regularly need to check. However, if you have to move around a lot, use different machines a lot and so on, you'd have to re-enter all your functions again and again to be able to use them. I even have to do it every time I exit and restart Powershell for some reason, as it won't remember the functions...
I've written a function that does this for me. I'm posting it here because I want to be certain it's foolproof.
The function itself is stored in allFunctions.ps1
, which is why I have it excluded in the code.
The basic idea is that you have one folder in which you store all your ps1 files which each include a function. In powershell, you go to that directory and then you enter:
. .\allFunctions.ps1
The contents of that script is this:
[string]$items = Get-ChildItem -Path . -Exclude allFunctions.ps1
$itemlist = $items.split(" ")
foreach($item in $itemlist)
{
. $item
}
This script will first collect every file in your directory, meaning all non-ps1 files you might have in there too. allFunctions.ps1 will be excluded.
Then I split the long string based on the space, which is the common seperator here.
And then I run through it with a Foreach-loop, each time initializing the function into Powershell.
Suppose you have over 100 functions and you never know which ones you'll need and which you won't? Why not enter them all instead of nitpicking?
So I'm wondering, what can go wrong here? I want this to be really safe, since I'm probably going to be using it a lot.