DEV Community

Cover image for How I Set Up an Awesome PowerShell Environment for Script Development
Emanuele Bartolesi for Playful Programming

Posted on Edited on

How I Set Up an Awesome PowerShell Environment for Script Development

I usually reach for PowerShell when I need to automate a task, troubleshoot something, or help a colleague with a script. I keep small modules for jobs I do repeatedly, like compressing a folder or working with CSV files. In the first version of this post, I imported all of them in my profile. That made them handy at the prompt, and it also meant a script could appear to work because I’d already loaded its dependencies before running it.

Updated November 18, 2026. Thanks to Mortiel for spotting the PSModulePath bug and Peter Koves for raising the question of explicit dependencies.

Keep personal modules easy to find

The examples use PowerShell 7 on Windows. If you’re setting up a new machine, PowerShell 7.6 is the current LTS release as of this update. Check which shell you’re in with $PSVersionTable.PSVersion, especially if Windows PowerShell 5.1 is also installed. PowerShell support lifecycle

My Utilities and Business folders give me a place to keep different kinds of helpers. If you don’t need that split, putting your modules in PowerShell’s default current-user module directory is simpler. For custom folders, each module needs its own directory. For example, ReportTools would live at PowerShell/Utilities/ReportTools/ReportTools.psm1 under your home directory. PowerShell module search paths

To use the same custom folders at the PowerShell prompt and in the VS Code extension’s PowerShell session, edit the current-user, all-hosts profile. Run this in PowerShell 7 to create the file if needed and open it in VS Code. PowerShell profile scopes

$profilePath = $PROFILE.CurrentUserAllHosts
if (-not (Test-Path -LiteralPath $profilePath)) {
    New-Item -ItemType File -Path $profilePath -Force | Out-Null
}
code $profilePath
Enter fullscreen mode Exit fullscreen mode

Add this to the profile and adapt the folder names to your machine.

$separator = [IO.Path]::PathSeparator
$moduleRoot = Join-Path $HOME 'PowerShell'
$modulePaths = $env:PSModulePath -split $separator

foreach ($folder in 'Utilities', 'Business') {
    $modulePath = Join-Path $moduleRoot $folder
    if ((Test-Path -LiteralPath $modulePath -PathType Container) -and
        $modulePath -notin $modulePaths) {
        $modulePaths += $modulePath
    }
}

$env:PSModulePath = $modulePaths -join $separator
Enter fullscreen mode Exit fullscreen mode

The original -contains check was comparing a single directory with the entire path string. Splitting the string first lets you check each entry and avoid appending the same path again. The Test-Path check lets the profile start normally if one of the folders is missing.

Restart the session after saving. In PowerShell’s default configuration, calling an exported command for the first time can load its module automatically. To check discovery without running the command, use Get-Module -ListAvailable -Name ReportTools after putting the module below in one of your module folders. PowerShell module autoloading

Give shared scripts their own dependencies

Take a small CSV inventory check. If it calls a helper that only exists in my Utilities folder, it can work at my prompt and fail on a colleague’s machine. For this example, I’d put the helper in the same repository as the script and import it by path.

inventory-check/
├── Get-InventoryCount.ps1
├── inventory.csv
└── Modules/
    └── ReportTools/
        └── ReportTools.psm1
Enter fullscreen mode Exit fullscreen mode

Save this as Modules/ReportTools/ReportTools.psm1.

function Get-InventoryRowCount {
    param([Parameter(Mandatory)][string]$Path)

    (Import-Csv -LiteralPath $Path -ErrorAction Stop | Measure-Object).Count
}

Export-ModuleMember -Function Get-InventoryRowCount
Enter fullscreen mode Exit fullscreen mode

Save this as Get-InventoryCount.ps1.

param([string]$Path = (Join-Path $PSScriptRoot 'inventory.csv'))

$modulePath = Join-Path $PSScriptRoot 'Modules/ReportTools/ReportTools.psm1'
Import-Module $modulePath -ErrorAction Stop

Get-InventoryRowCount -Path $Path
Enter fullscreen mode Exit fullscreen mode

$PSScriptRoot points to the directory containing the script, so the module and default CSV resolve even if you run the script from a different directory. If your script depends on a module installed separately, #Requires -Modules can declare that dependency and stop the script before it runs if the module can’t be loaded. If the script needs PowerShell classes or enums from the module, look at using module. Import-Module · about_Requires · about_Using

Put this in inventory.csv.

Machine,Status
build-01,Ready
build-02,Maintenance
Enter fullscreen mode Exit fullscreen mode

Run this from the repository directory.

pwsh -NoProfile -File ./Get-InventoryCount.ps1
Enter fullscreen mode Exit fullscreen mode

You should see 2. -NoProfile skips PowerShell profiles, and the process can still inherit environment variables from its parent shell. For a stronger handoff check, run it in a clean checkout on another machine or a CI runner with only the documented prerequisites installed. PowerShell command-line options

Debug the same script in VS Code

With Microsoft’s PowerShell extension installed, open Get-InventoryCount.ps1, put the cursor on the Import-Module line, and press F9 to set a breakpoint. Press F5 to debug the current script. When execution pauses, inspect $modulePath in the Variables pane. If you’ve accidentally pointed to a folder on your own machine, you can see it before the import runs. The extension also surfaces PSScriptAnalyzer findings in the Problems panel while you edit. Debugging PowerShell in VS Code · PSScriptAnalyzer integration

I’d keep Utilities and Business for the work I do at the prompt. Before sharing a script, I’d check that someone starting with the repo and its documented prerequisites can run it. That’s a better result than seeing a list of modules load every time I open a terminal.

For the terminal itself, see my earlier post, Transforming Windows Terminal into a Productivity Powerhouse.


This revision was prepared with AI assistance. The PowerShell examples were tested and the technical guidance was checked against Microsoft’s documentation.

For more articles on developer tooling and automation, subscribe to Dev Dispatch.

Top comments (9)

Collapse
 
kovesp profile image
Peter Koves • • Edited

IMHO, it is better to have 'using module ModuleName' for the modules used in a script. I want to know the dependencies of each script. As well, using takes effect at compilation time, wheras Import-Module happens at run-time.

For 'using module ...' to work seamlessly (using your environment as an example) you need to

  1. Have Utilities and Business on PSModulePath
  2. Each module must be in its own directory under either Utilities or Business.
    • The directory must have the same name as the PSM1 file.
    • The directory must contain the PSM1 file as well as the corresponding PSD1 file.
    • The PSD1 file can be generated by the New-ModuleManifest cmdlet.
    • At the minimum, set the RootModuleentry to the name of the PSM1 file.
    • Each function must be listed in the FunctionsToExportentry. The other ...ToExport entries should be self-explanatory. Classes do not need an entry.

If you insist on importing everything from the profile you can use

foreach ($path in @('Utilites','Business') {
   foreach ($mod in Get-ChildItem $path -Directory) {
      Invoke-Expression "using module $($mod.Name)"
   }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mortiel profile image
Mortiel • • Edited

There is upsides and downsides to "using module..." that I've found.

Mainly, "using module" is absolutely required if you use custom classes in a script module, as custom classes do not load with Import-Module.

However, if you have to script using Windows PowerShell 5.1 (example: scripting for Windows Server with security restrictions), I've encountered bugs with "using module" that were fixed in later PowerShell Core versions.

I suppose that's an argument for creating compiled DLL modules, but I digress.

The main takeaway is that you should be very selective with the custom modules you load using the PSProfile as opposed to in the actual scripts, but instead only adding custom modules directories to PSModulePath is generally good to make "using" or "Import-Module" not require a full directory path.

Collapse
 
kovesp profile image
Peter Koves •

Agreed. I was using Import-Module until I placed my first class in a module and spent a few hours trying to understand why my class was not being seen.

The fact that having using module in a script will not reload the modified class when running the script in a session still bites. If only functions/cmdlets are modified in the module, one can use Import-Module -Force (ipmo -fo) to reload the module. But if a class is changed, a new session is needed. Which is a pain in VS Code: 3 clicks to restart the session + losing variables set in it.

The only alternative is to select the class and use F8. But selecting a class isn't quite straight forward in VS Code; at best it involves using Expand-Selection (Alt-Shift-RightArrow) multiple times.

Thread Thread
 
mortiel profile image
Mortiel •

Using VS Code, you can pin the PowerShell menu to the status bar, which reduces the number of clicks needed down to 2, but better than using ISE, I suppose.

The other method is writing the module in .NET/C# as a DLL, which allows creating namespaces and more class capabilities. Sadly, I'm not extremely knowledgeable with C# and haven't really needed to go that route yet.

Collapse
 
pascal_sala_6-25-24 profile image
Pascal Sala •

Thank you, good article.

Collapse
 
kasuken profile image
Emanuele Bartolesi Playful Programming •

thanks for the feedback.

Collapse
 
mortiel profile image
Mortiel • • Edited

If I remember correctly, I think the environmental variable $env:PSModulePath is a string value not an array/collection, so technically the if condition that uses -contains won't work as intended. You have to split the variable by the ';' delimiter into an array first.
$PSModulePathArray = $env:PSModulePath -split ';'

However, you're not seeing issues because the if condition is a negative match... "If ($string1
-contains $string2)" will always be false, so the negative of that will always be true, yeah?

Restart a PowerShell session multiple times and check if your custom "Business" and "Utilities" paths are in the PSModulePath variable multiple times.

Collapse
 
moodcave profile image
David •

Fyi, looks like your ModuleDefinitions have the paths flipped in that code snippet.

Collapse
 
kasuken profile image
Emanuele Bartolesi Playful Programming •

thanks for that!
I never saw it in my script, because at the end, it works anyway! 😂😂😂