I'm writing CI/CD scripts in PowerShell and split larger scripts into separate .ps1 files to keep them organized. For example, a paths.ps1 script creates a $Paths PSCustomObject containing shared directories, and build.ps1 dot-sources that file before using $Paths.BuildRootDir, $Paths.SrcDir, and other properties. The code runs correctly, but the editor doesn't offer IntelliSense or property completion after typing $Paths. Is there a practical workaround for getting autocomplete on objects created this way?
3 Answers
If the shared code is mostly functions rather than data objects, consider turning it into a PowerShell module. Put the functions in a .psm1 file and load the module from the standard module path. The editor can generally discover and complete exported functions like regular cmdlets.
Make sure you’re testing completion in the PowerShell extension’s integrated or debug terminal. Completion may be available there even when it doesn’t appear in a regular terminal session.
PowerShell completion generally gets information either from the current session state or by statically analyzing the script. Static analysis usually can’t determine the properties of a variable assigned inside a dot-sourced script. One workaround is to dot-source paths.ps1 in the editor’s integrated PowerShell/debug console before working, which places $Paths into the session and allows completion there. Another option is to make paths.ps1 output the object instead of assigning it, then capture it in the consuming script, although runtime variables such as $PSScriptRoot can still prevent static analysis from resolving everything.

Dot-sourcing the file in the integrated console works well enough for my workflow. Thanks for explaining why the editor can’t infer the properties.