How to Fix PowerShell Window Closing When You Run the Script

author avatar

0 Followers
How to Fix PowerShell Window Closing When You Run the Script

PowerShell works as a scripting language and the command-line shell. It is one of the reasons why PowerShell is a favorite of a lot of users who like to set the task of automatic management and process to the operating system.

However, users have faced troubles in this application too, such as it closes after running some script. So, users try to keep the windows open even after running the script in PowerShell. This problem becomes essential to address when you remotely access the other server with the help of shared login credentials.

If you wish to remove this problem, then you can use these methods:

Initiate the Connection for the Remote Access

Come to your home screen desktop.Create a file on desktop and name it executes .ps1Open this file and paste the following content in it.clear-Host

$br = “`n”;$br

Write-Output ‘Step1: Press 1 for Powershell to reload.’Write-Output ‘Step2: Press 2 to start a remote session.’

$br

Write-Host ‘This script will’ -NoNewline -ForegroundColor GreenWrite-Host ‘ not ‘ -ForegroundColor Red -BackgroundColor Black -NoNewlineWrite-Host ‘indicate which step you are on,’ -ForegroundColor GreenWrite-Host ‘if you are not sure, rather press 1 again and then 2.’ -ForegroundColor Green

$br

$input = Read-Host ‘Enter your choice’

switch($input){

# Change the name of the execute.ps1 if you want to

1{powershell.exe -noexit “C:users$env:usernameDesktopexecute.ps1” -noprofile}

#

2{

Clear-Host;Write-Warning ‘You are about to connect to the remote server’

# Change the ‘RemoteServerName’ to the name of your server and change the ‘domainname’ to your actual domain name

Enter-PSSession -ComputerName RemoteServerName -Credential “domainname$env:username”

}

default{Write-Warning ‘An error has occurred’}

}

Run this algorithm in the PowerShell.When you run it for the first time, you have to press the 1, which opens it and the No Exit Switch doesn’t let it shut down.For the second time, you have to press 2, which allows the connection for the remote access.

Pause Command

If you want to avoid the closing of the application, then you can try the pause command. If you use this command, it initiates the new line of a command interpreter, which will run as a background command.

This command can make PowerShell window wait till you give the further command about what to do. To apply this command you have to use the following command line: Cmd /c pause

Command to End the Script

One of the quick tricks is to end the scripts with the addition of a line. You have to enter the following command:

Read-Host -Prompt “Press Enter to exit”

This command works under PS-ISE, and this command is much effective to stop the shutting down of the PowerShell window.

Use Local Network to on a remote PC for the PowerShell script

To access the remote control, you have to create the open.ps1, which can be done by the following command:

powershell.exe -noexit c:remote.ps1

To access the remote control of the PC, you can use the PowerShell command:

Enter-PSSession -ComputerName YourExchangeServer -Credential Get-Credential

Use of Start-Sleep Command Outlet

When you are facing the problem like shutting off the application after running any script then you can use this command which can resolve the PowerShell issue with this command:

Start-Sleep -s 15

This command ceases the script action for a fixed period, and at this time, you can perform other tasks.

These are the tricks that can help you when you are facing such an issue. PowerShell can be useful when users have plenty of tasks to perform. Here you can keep working without waiting for the previous task to complete.

Source url:- https://williamsblogpoint.wordpress.com/2020/03/16/how-to-fix-powershell-window-closing-when-you-run-the-script/

Top
Comments (0)
Login to post.