0

My setup:

One Powershell script with a menu. Upon selecting a a menu item, a specific Powershell script is run assigned to that menu item. I'm attempting to make this second Powershell script run silently as in - nothing is shown in the main Powershell script.

Bonus: The full setup I'm looking at is:

  • User runs Powershell script
  • User selects a menu item (this executes another Powershell script)
  • The information in the other Powershell script is not shown to the user
  • After the second Powershell script has been executed, the user is shown a line of text such as: "Script has been run successfully"

What I've tried: Adding the following code to each secondary script

$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)

Unfortunately this closes the main Powershell window which isn't what I desire.

2 Answers 2

2

You could run the secondary command as a background-job and wait for it to finish. That way no output is shown in the shell

Start-Job -FilePath YourSecondaryScript.ps1 | Wait-Job

if you need, you can still analyze the job result (the script output)

Sign up to request clarification or add additional context in comments.

Comments

1

Fixed by appending

| out-null

at the end of each line of code in the secondary scripts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.