Devops

How To Start A New Instance In Powershell?

Description of the image

PowerShell is a more advanced shell used to administer Windows operating systems using the MS.Net framework. It allows you to schedule tasks to be accomplished within an operating time, control and govern the processors and memory, and interact with other applications and services. Among these, the creation of new PowerShell sessions, which are used to execute some commands and scripts away from the artist session, is frequently relied on.

Understanding PowerShell Instances   

PowerShell can be opened in two ways: affiliated or new. When opened, an instance is created for you. It has its own memory space, flags, execution context, environment variables, and other things. You will find that when you type in commands or scripts to execute in this instance, they will not impact any other instances or the main system environment.   

Methods For Launching New Powershell Instances

Start-Process cmdlet

The start-process cmdlet is the most versatile and customizable approach for launching new PowerShell instances. It allows you to specify various parameters to control the behaviour of the new process, including:

-FilePath

Path to the PowerShell executable (powershell.exe).

-ArgumentList

  • List of arguments to pass to the new instances, such as scripts or commands to execute.
  • Powershell
  • Start-Process powershell.exe -ArgumentList “-ExecutionPolicy Bypass -File C:\MyScript.ps1”

-NoNewWindow

It prevents a new window from opening, keeping the process within the current console.

-NoExit

  • Keep the new window open after the script finishes.
  • PowerShell
  • Start-Process powershell.exe -ArgumentList “-NoExit”, “-noprofile” “-command Get-Process”

-WindowStyle

Allows the window style for the current form or a particular form by using commands such as normal, maximized, or minimized.

PowerShell

Start-Process powershell.exe -WindowStyle Maximized

-UseShellExecutive

Launches PowerShell using the default shell execution method.

For Example:

Start-Process powershell.exe -ArgumentList “-ExecutionPolicy Bypass -File C: from the cmd.exe, open PowerShell and run the script “MyScript.ps1” in the latest window with the statement: -WindowStyle Maximized

 

This command starts a new PowerShell instance in  a maximized window, bypassing the execution policy to run the script C:\MyScript.ps1

Invoke-Expression Cmdlet

The Invoke-Expression cmdlet directly executes a string as a PowerShell command within a new process. This method is simpler but offers less control compared to the Start-Process.

Example:

Invoke-Expression “(Start-Process powershell.exe -Argument List ‘-noprofile’, ‘-command Get-Date’)”

This command launches a new PowerShell instance and retrieves a list of running processes.

Keyboard Shortcuts

For quick access, you can use keyboard shortcuts to open new PowerShell instances:

  • Hold shift while clicking the Start menu and selecting “Windows Powershell”.
  • Press Win+R, type “powershell”, and press Enter.
  • Scripting techniques:- You can also leverage PowerShell scripts to automate the process of instances with desired configurations. Here is an example: 
function Start-NewPSInstance {
param (
[Paramater (Mandatory = $true)]
[string] $ScriptPath,
[string] $Arguments = “ “
)
Start-Process powershell.exe -ArgumentList “-noprofile”, “-ExecutionPolicy Bypass”, “-File $ScriptPath”, $Arguments
}
                       Start-NewPSInstance -ScriptPath “C:\MyLongRunningScript.ps1” -Arguments “-verbose” 

In scripts, you can leverage various methods to launch new PowerShell instances depending on your specific needs:

Calling Start-Process within a script:

This allows programmatic control over launching new instances with desired parameters.

Using the dot (.) operator:

The dot operator (.) executes a command in a child process, essentially creating a new instance for that specific command.

For Example:   

Start-Process powershell.exe-ArgumentList “-noprofile -command Get-Date”

 # Using the dot operator

Get-ChildItem. -Filter “*.txt”

The first line launches a new instance to display the current date. The second line uses the dot operator to list text files within the current directory in a separate process.

Choosing The Right Method

Considering your working environment and your goals, it is probably the most reasonable thing to determine the best way to start a new PowerShell instance.

  • Start-Process:- Overall, the most flexible is the Start-Process cmdlet, which has parameters based on usage.
  • Invoke-Expression:- Offers more dynamic for scripting in scripting situations.
  • Keyboard Shortcuts: Keyboard shortcuts are sometimes easy to use, as they require one to type a letter or press a specific button to get the required result.
  • Scripting:- Scripting makes it possible to automate processes, apply templates or customize them.

Such methods will help you promptly start new instances of Powershell for your tasks, improving your scripting/automation game.

Advanced Considerations

Remote execution:

When using RS, you can use the Invoke-Command cmdlet to run commands or scripts on distant systems within the same network, which has the effect of starting new PowerShell sessions on the given machines.

Security:

Remember not to type Invoke-Expression or execute other code in scenarios where standard execution policy is to be utilized. By default, it is recommended that user input be checked and sanitized before being fed to new instances of the programs.

Execution Policy:

By default, Powershell restricts script execution for security reasons. Use the -ExecutionPolicy Bypass parameter cautiously or adjust the global execution policy for more control.

Elevated Privileges:

Use administrator privileges only when necessary to avoid unintended system modifications.

Examples and use cases:

Running long-running scripts:

Start a new instance with - NoExit to keep the window open while a lengthy script executes, allowing you to monitor progress or interact with the new session if needed.

Isolating Script Execution:

Launch a new instance with specific execution policies or environment variables to ensure your script runs in a controlled environment without affecting the current session.

Automating tasks with scheduled jobs:

Use Start-Process within scheduled tasks to trigger Powershell scripts at specific times or events, running them in isolation from interactive sessions.

Remote management and script deployment:

Combine Invoke-command with Start-Process to remotely launch Powershell instances on multiple machines and execute deployment or configuration scripts.

Mastering PowerShell Instances

Understanding how to launch new PowerShell instances empowers you to manage tasks efficiently and execute specific commands or scripts in isolated environments. Combining the different methods and advanced considerations discussed in this post, you can tailor your approach to various automation and scripting scenarios with your PowerShell workflows.

Read More

https://devopsden.io/article/amazon-q

Follow us on

https://www.linkedin.com/company/devopsden/

Table of Contents

    Subscribe to Us

    Always Get Notified