How does more experienced PowerShell scripter's do this?

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Ronnie Jorgensen Ronnie Jorgensen 5 months ago.

  • Author
    Posts
  • #46021
    Profile photo of Ronnie Jorgensen
    Ronnie Jorgensen
    Participant

    Hi all,

    Sorry for the very vague topic title, but i wanted a title that sort of referenced what i was going to ask for without it being too long.

    So "How does more experienced PowerShell scripter's do this" ??

    I would like to ask how does more experienced scripters work with the PowerShell console when running scripts and some script depends on other scripts etc etc.

    So to give a short background. I tend to have a PowerShell profile that more or less just say:

    Set-Location -Path C:\Scripts

    I do that because i like to land in my script folder. where i keep subfolders for other scripts or able to keep function ps1 files etc for easier use.

    So i know that i can . source ps1 files in either a another script file or in my PowerShell Profile script file so that i can take advantage of calling functions easier like Connect-EXhangeOnPrem without the need of doing "Set-Location -Path "C:\Scripts\Connect-ExchangeOnPrem" after which now i can run .\Connect-ExchangeOnPrem.

    So yes i can . source in my profile or in a script to easier get functions from another ps1 file. but today i learned that if you have a script named Get-Link.PS1 and you put in in a folder that is in your environment PATH then you can be in C:\ location and still run .\Get-Link which i thought ok that is neat.

    So how do more experienced scripters do this? I guess we will never get away from . sourcing files into scripts as they needs dependencies as they run. But if you are just wanting to open your console and be able to work easier with functions and other scripts that are in your script repo, what do people do?

  • #46028
    Profile photo of TomBaker
    TomBaker
    Participant

    Modify $psmodulepath to include c:\scripts which is a git repo.
    Make sub folders organizing common scripts into modules.
    Use new-modulemanifest to create psd1 files for each module.

    Enjoy automatic module loading when you use a function for the first time.

    When 'updating the functions' a loaded module will keep the Old version cached. Make use of import-module -force in those cases.

  • #46030
    Profile photo of Trevor Freedland
    Trevor Freedland
    Participant

    I added a new directory path to $env:PSModulePath for completed modules that I use often but are still being actively developed. Final modules I am not developing I put in a proper module path; C:\Users\$env:USERNAME\Documents\WindowsPowerShell\Modules.

    I also have a simple function in my PS Profile called Switch-Proj, which just cd's me into my default project directory (a long filepath). From there I can quickly move into any project folder, import a module in progress, etc. I prefer to use a helper function because I can call it on the fly..

    Regarding setting dependencies in scripts, the #Requires statement will prevent a script from running without the required elements and can auto load required modules in PS 3.0 and later. There is a good Scripting Games tip on this topic. The about_Requires help file is really good too.

  • #46038
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant
  • #46047
    Profile photo of Ronnie Jorgensen
    Ronnie Jorgensen
    Participant

    Thank you everyone, I will start looking into that and hopefully this will help me one way or another 🙂

    Thank you again for the pointers, do really appreciate it.

You must be logged in to reply to this topic.