How can I call a ps1 within a ps1?

This topic contains 4 replies, has 3 voices, and was last updated by  Darren Rolfe 3 weeks ago.

  • Author
    Posts
  • #100102

    Darren Rolfe
    Participant

    Hello Everyone,

    I'm trying to create a menu system using PowerShell.

    This is achievable as per this URL:

    http://www.tomsitpro.com/articles/powershell-interactive-menu,2-961.html

    What I need to do next is have the ability to customize the menu so I can call another ps1 file.

    For example adding this ps1:

    https://gallery.technet.microsoft.com/office/AD-and-mailbox-from-CSV-96a4713f

    Do I use dot notation? If so, how do I achieve that?

    Or should I use some other method?

    Sorry if this is a basic question. I have tried to investigate but have not been able to come up with anything.

    Regards, Darren

  • #100105

    Simon B
    Participant

    Assuming the script you wish to run is in the same directory as you main script, you could do something like this in your on click event

    Invoke-Expression .\powershellscripttorun.ps1

    • #100114

      Darren Rolfe
      Participant

      Thank you Simon B and postanote. I presumed it was something simple. Live and learn. Much appreciated. 🙂

  • #100111

    postanote
    Participant

    All you need in in the scripts you point to.
    All you need to do is add a call to the script.
    There are several ways to do that

    From your sample pointers

        function Show-Menu
         {
              param (
                    [string]$Title = 'My Menu'
              )
              cls
              Write-Host "================ $Title ================"
          
              Write-Host "1: Press '1' for this option."
              Write-Host "2: Press '2' for this option."
              Write-Host "3: Press '3' for this option."
              Write-Host "Q: Press 'Q' to quit."
         }
    
        do
        {
              Show-Menu
              $input = Read-Host "Please make a selection"
              switch ($input)
              {
                    '1' {
                         cls
                         'You chose option #1'
                         # Run this script
                         Start-Process -FilePath powershell 'D:\Scripts\HelloWorld.ps1' -Wait
                    } '2' {
                         cls
                         'You chose option #2'
                         Invoke-Expression -Command 'D:\Scripts\HelloWorld.ps1'
                    } '3' {
                         cls
                         'You chose option #3'
                         & 'D:\Scripts\HelloWorld.ps1'
                    } 'q' {
                         return
                    }
              }
              pause
         }
         until ($input -eq 'q')
    
    
    • #100116

      Darren Rolfe
      Participant

      Thank you postanote. I presumed it was something simple. Live and learn. Much appreciated. 🙂

You must be logged in to reply to this topic.