Need help with simple script

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?

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.

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

Set-Location $drive
Get-ChildItem -Filter *$filename* -Recurse | Invoke-Item

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

$drive = “C:\Temp”
Set-Location $
$file = $quit = $null

do
{
    $filename = read-host “Input PO Number or press 'Q' to exit”
    
    if($filename -eq 'Q'){
        $quit = $true
    } else {
        $file = Get-ChildItem -Filter *$filename* -Recurse | Out-GridView -Title "Confirm file selection" -PassThru
    }
}
until($file -or $quit)

if($quit){
    Write-Host User cancelled -ForegroundColor Cyan;\
    Start-Sleep -Seconds 3
    break
}

$file | Invoke-Item

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

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