Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load all functions into PowerShell from a certain directory

Tags:

powershell

Suppose you're a system administrator who uses PowerShell to manage 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 separator 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.

like image 587
KdgDev Avatar asked Apr 18 '09 17:04

KdgDev


People also ask

How do I read all files in a directory in PowerShell?

PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function. In this article, we have compiled examples to understand listing files in PowerShell.

How do I import a PowerShell script module?

In PowerShell 2.0, you can import a newly-installed PowerShell module with a call to Import-Module cmdlet. In PowerShell 3.0, PowerShell is able to implicitly import a module when one of the functions or cmdlets in the module is called by a user.

How do you call a function in PowerShell?

A function in PowerShell is declared with the function keyword followed by the function name and then an open and closing curly brace. The code that the function will execute is contained within those curly braces. The function shown is a simple example that returns the version of PowerShell.


1 Answers

Include them in your PowerShell profile so they will load automatically every time you start PS.

Look at Windows PowerShell Profiles for more info about where to find your profile script.

PS defaults your profile to your "My Documents" folder. Mine is on a network drive, so anywhere I login, PowerShell points to the same profile folder.

like image 121
aphoria Avatar answered Sep 21 '22 21:09

aphoria