0

I have a script called Restart-Audio.ps1

Stop-Process -Confirm -Name musicapp*
Stop-Service audiosrv
Stop-Service AudioEndpointBuilder
Start-Service audiosrv
Invoke-Item C:\path\to\music\app.exe

Every time I start a new PowerShell session, I want this script to run when I type "Restart-Audio" like a normal cmdlet. When I tried to add this to my PowerShell profile, it tried to run the script, asking for the confirmation to stop the processes. I don't want it to run as soon as a PowerShell session starts; I want the command to run when I tell it to.

Thanks.

1 Answer 1

2

This can be done using Powershell profiles, see here for details.

You basically create (if the file doesn't already exist) C:\Users\<<UserName>>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 and that can contain your above script.

I'd construct the file as:

Function Restart-Audio{
Stop-Process -Confirm -Name musicapp*
Stop-Service audiosrv
Stop-Service AudioEndpointBuilder
Start-Service audiosrv
Invoke-Item C:\path\to\music\app.exe
}

Then you just open Powershell and type Restart-Audio to run that function.

Additionally, if you save the script to any folder in your $env:PATH you can just type the first few characters of the script name and TAB complete to run the script.

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

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.