Need help with simple script

Welcome Forums General PowerShell Q&A Need help with simple script

Viewing 3 reply threads
  • Author
    Posts
    • #266600
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      I am new so please dont beat me up too bad. I am trying to write a small script that a user can input a PO number find and open then file then exit

       

      this is what i have so far

       

      $drive = “C:\Temp”
      $filename = read-host “Input PO Number”

      Set-Location $drive
      Get-ChildItem -Include:$filename -Recurse
      Invoke-Item “$filename”

      Exit

       

      Am I on the right track?

    • #266624
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      Seems you are on the right track. I assume the PO is in the filename based on your logic. I would recommend using -Filter instead of -Include. Invoke-Item will open with the program associated with the file extension on the user’s computer. You may need to be more specific if these associations aren’t what you’re expecting. Finally, the exit is not required. When the script is complete, it will close out.

      Note that the search may return more than one match, especially if the users types something vague. Not that users ever do anything ridiculous or unexpected. You could do something like a repeating loop until the user either picks one file or press Q to exit, for example. If you combine that with Out-GridView it could be a pleasant experience for the user with a GUI. Now that I think of it, you could present them with this regardless so they cacn confirm they are going to open the correct file.

      Here’s a rough draft of such a script

    • #266627
      Participant
      Topics: 1
      Replies: 1
      Points: 15
      Rank: Member

      thank you so much for your help, I will try this out!!!!

    • #266633
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      It is my pleasure. Hopefully it’s helpful with achieving your desired result.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.