72

(I can't believe I'm actually asking this, but I am out of brainpower for the day.)

I just wrote my first serious PowerShell script, and I'm really happy with it. I plan to use it every day or so. I want to be able to call it from the Posh command line. I'll give it a verb-noun type name, but for now it's a simple .ps1 and not one of those fancy advanced functions that take params and such.

So where should it go and how do I call it from a Posh command line? I plan to write more! Where should they go?

  • Should it be a function in my profile?
  • Should it go on my path?
  • Does it go in a PSMODULEPATH? What kind of stuff goes there anyway? Does it look recursively or is it just like the normal PATH?

Where do you all put your PowerShell scripts and how do you organize them? I've got a lot of experience making C# and C++ tools and know what to name them and where to put them. And at the other extreme I've done a lot of crappy .bat files which are usually standalone or piled in a heap in some folder. But PowerShell seems to be very different. You can make crappy .bat file type things in it really quickly, or you can be building libraries and sophisticated services with it.

I'd really love some ideas on how I should start organizing these things before I start. Obviously everybody is different, so I'm hoping for some discussion. Thanks!

scobi
  • 14,252
  • 13
  • 80
  • 114

6 Answers6

28

I put my personal scripts in the same folder as my profile. I can then back up & version them together. My profile begins with:

$ProfileRoot = (Split-Path -Parent $MyInvocation.MyCommand.Path)
$env:path += ";$ProfileRoot"
Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
  • 2
    use $script:MyInvocation.MyCommand.Path, see [source path of an executing script](http://stackoverflow.com/questions/801967/how-can-i-find-the-source-path-of-an-executing-script/6985381#6985381). You get into problems with nested callers, even with the profile script, for example I have a helper function to reload your profile (after function/variable edits) but skip all one-time init (like loading .NET assemblies). – yzorg May 31 '12 at 18:32
  • 1
    Is this script idempotent? Or would you potentially be re-appending the same directory over and over each time you ran it? – Ross Brasseaux Apr 15 '21 at 15:26
19

My recommendations: - Store the script in a directory as you wish, e.g. c:\posh - Add the directory to $env:path

$env:path += ";c:\posh"

This ensures that you may be in other directory, say c:\windows, but you can call the script

[c:\windows] > sampl[TAB] # it expands the name of file to sample.ps1, then hit enter

If your file sample.ps1 contains functions definitions and you import it every time, then I would consider adding this line to your $profile file

. c:\posh\sample.ps1

Concerning script organization.. just several dirs according to the purpose of the scripts :) Personal, dev, external (downloaded), samples,...

stej
  • 28,745
  • 11
  • 71
  • 104
  • 1
    This is the most straightforward answer, and it works. Plus it doesn't screw up your personal organization. As far as backups, etc. These are very custom, and so its case by case where you should store scripts for backup, etc. Plus "backup" isn't really pertinent to the question :). – Decoded Jan 10 '21 at 17:16
19

With V2, you can create a modules directory in the WindowsPowerShell directory where your profile is. PS will automatically look in that directory to load modules when you run import-module. I created a "Scripts" directory under WindowsPowerShell as well that is a sibling directory of Modules.

I use my profile to set some directories using variables with the following code:

PS>  cat $Profile
$scripts = "$(split-path $profile)\Scripts"
$modules = "$(split-path $profile)\Modules"
$docs    =  $(resolve-path "$Env:userprofile\documents")
$desktop =  $(resolve-path "$Env:userprofile\desktop")

PS> cat variable:\scripts
C:\Users\andy.schneider\Documents\WindowsPowerShell\Scripts

PS>  cat variable:\modules
C:\Users\andy.schneider\Documents\WindowsPowerShell\Modules
Andy Schneider
  • 8,516
  • 6
  • 36
  • 52
  • This is what I do as well. Though I also have a "Libraries" directory with scripts that my profile auto executes to define global functions. I did that in PowerShell v1, I will probably replace all that with Modules eventually. – JasonMArcher Jun 28 '09 at 00:02
10

This is what I do:

note: substitute "ModuleName" for something meaningful.

Create a module and save it in the global modules folder as "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ModuleName\ModuleName.psm1". e.g.:

function global:FancyFunction() {
   # do something interesting here.
}

Export-ModuleMember -function FancyFunction 

Open your powershell profile and add the following line to make sure that your module is loaded every time you start a powershell session:

Import-Module ModuleName -Force

You can easiliy find your powershell profile by typing:

notepad $profile

When you open a new powershell session, you should be able to call your function from the console or from other scripts without having to do anything else.

Andrew Jackson
  • 764
  • 1
  • 7
  • 13
  • `C:\Windows\System32\WindowsPowerShell\v1.0\Modules` is now a legacy path and shouldn't be used any more. – Dennis Sep 01 '23 at 09:45
2

There is an exported variable in PowerShellGet module used in powershell 7.1

$PSGetPath

it has 4 properties

AllUsersModules    : C:\Program Files\PowerShell\Modules
AllUsersScripts    : C:\Program Files\PowerShell\Scripts
CurrentUserModules : C:\Users\username\Documents\PowerShell\Modules
CurrentUserScripts : C:\Users\username\Documents\PowerShell\Scripts

you could use one of those script locations. Install-Script is using them also.

maciejW
  • 122
  • 1
  • 1
  • 8
0

Normally you would distribute your code using advanced functions in modules. But if you for some obscure reason don't want to have your scripts to behave as built-in commands, you can distribute script files instead.

Scripts can be installed from a registered PSRepository (such as PS Gallery or one of your own, see Register-PSRepository) using the command Install-Script.

Install-Script -Repository PSGallery -Name Write-HelloWorld

You can select the installation directory to be used by specyfing -Scope

  • -Scope CurrentUser will install the script to
    $ENV:UserProfile\Documents\WindowsPowerShell\Scripts
  • -Scope AllUsers will install the script to
    $ENV:ProgramFiles\WindowsPowerShell\Scripts

For PowerShell 7.x the paths will be

  • -Scope CurrentUser will install the script to
    $ENV:UserProfile\Documents\PowerShell\Scripts
  • -Scope AllUsers will install the script to
    $ENV:ProgramFiles\PowerShell\Scripts

(The scripts folder doesn't exist for PowerShell 7 untill you try installing a script for the first time.)

Instead of using a PowerShell repository and Install-Script you could of course copy the script directly to the preferred script folder.

The difference will be that Get-InstalledScript will only display scripts that got the corresponding XML-description file, copied to the folder InstalledScriptInfos, as part of Install-Script command usage.

Dennis
  • 871
  • 9
  • 29