Where to put PowerShell scripts?

(I cannot believe that I am actually asking about this, but I am not able to do it during 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 will give it a noun verb type name, but at the moment it is a simple .ps1, and not one of those fancy advanced functions that take parameters, etc.

So where should he go and what should I call him from the Posh command line? I plan to write more! Where should they go?

  • Should there be a function in my profile?
  • Should he go my way?
  • Does this happen in PSMODULEPATH? What is happening there? Does it look recursive or is it like a regular PATH?

Where do you all put your PowerShell scripts and how to organize them? I have a lot of experience creating C # and C ++ tools and know how to name them and where to place them. And on the other hand, I made a lot of crappy .bat files that are usually standalone or heaped in some kind of folder. But PowerShell seems very different. You can make a crappy file like file in it very quickly, or you can create libraries and complex services with it.

I would really like some ideas on how I should start organizing these things before I begin. Obviously, everyone is different, so I hope for some discussion. Thank!

+43
powershell
Jun 18 '09 at 7:22
source share
4 answers

I put my personal scripts in the same folder as my profile. Then I can back up and release them together. My profile starts with:

$ProfileRoot = (Split-Path -Parent $MyInvocation.MyCommand.Path) $env:path += ";$ProfileRoot" 
+22
Jun 19 '09 at 2:58 p.m.
source share
— -

With V2, you can create a module directory in the WindowsPowerShell directory where your profile is located. PS will automatically search in this directory for loading modules when import-module starts. I also created the "Scripts" directory under WindowsPowerShell, which is the directory of the children of the modules.

I use my profile to install 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 
+12
Jun 20 '09 at 16:09
source share

My recommendations: - Store the script in a directory as you wish; C: \ gorgeous - Add the directory to $ env: path

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

This ensures that you can be in a different directory, for example 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 sample.ps1 file contains function definitions and you import them every time, I would think about adding this line to the $ profile file

 . c:\posh\sample.ps1 

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

+8
Jun 18 '09 at 8:06
source share

This is what I do:

Note: replace "ModuleName" with something meaningful.

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

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

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

 Import-Module ModuleName -Force 

You can easily 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 doing anything else.

+7
Oct 01 '09 at 2:26
source share



All Articles