Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / How to Run Multiple Commands in One Line in PowerShell and CMD?

August 25, 2022 PowerShellWindows 10Windows Server 2019

How to Run Multiple Commands in One Line in PowerShell and CMD?

In this quick note, I’ll show you how to run multiple cmd or PowerShell commands in one line. Sometimes you have to do this when you invoke PowerShell commands from external programs, Windows Task Scheduler, logon scripts, when you need to bypass PowerShell Execution Policy, or when you have to quickly pass a piece of code to a user to diagnose/troubleshoot issues on their computer.

Executing Multiple Commands in One Line in Windows CMD

You can use the & cmd operator to run several commands one after another in one line. For example:

net stop wuauserv & net start wuauserv & wuauclt /detectnow

run multiple commands one after another in cmd

If you want the second command to be executed only if the previous one was successful, use the && format:

ipconfig /flushdns && ipconfig /renew

You can use a reverse condition: the second command will be executed only if the first one returned an error:

net stop wuauserv6 || net start wuauserv

Run Multiple PowerShell Commands in One Line

If you use an ampersand symbol to separate commands in one line, the following error appears:

 The ampersand (&) character is not allowed. The & operator is reserved for future use.

Instead of &, a semicolon ( ; ) is used in PowerShell to separate commands in one line:

restart-service wuauserv -verbose; get-process notepad

chain multiple PowerShell commands on one line

This one-liner PowerShell command will restart the Windows service and after the first command completes, it will list the information about the Windows process.

A popular example of a command abbreviation in PowerShell is a pipe, which allows you to pass the result of one command to the next one using a pipeline. In PowerShell, the “|” symbol is used as a pipeline operator. Here is an example of a pipeline command:

Get-Service -Name wususerv | Stop-Service

The same code can be written entirely in several lines:

$service = Get-Service -Name wususerv
$serviceName = $service.Name
Stop-Service -Name $serviceName

The pipeline allows to reduce the code size, and the code became more simple and readable.

To run multiple PowerShell commands using Start -> Run menu, use the format below:

powershell -command &"{ipconfig /all; ping woshub.com; pause "Press any key to continue"}"

If you run the same commands via the PowerShell console, use the following syntax:

powershell -command {ipconfig /all; ping woshub.com; pause "Press any key to continue"}

When running multiple PowerShell commands through the Windows Task Scheduler, you can use this format:

c:\windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Get-EventLog -LogName security; ipconfig /all ; get-service servicename}"

New && and || operators are available in modern PowerShell Core version 7.x. They work the same as in cmd.

You can check the PowerShell version by running $PSVersiontable.

The && operator runs PowerShell commands to the right of it if the command on the left has been successful:

Write-host "Hello!" && Write-host "World!"

&& powershell operator: will execute the right-hand pipeline if the left-hand succeeded

The || operator is used when you want to run a command if the previous command returned an error:

get-service servicename || write-host "missing service"

conditionally chain pipeline in powershell 7

0 comment
0
Facebook Twitter Google + Pinterest
previous post
How to Run a Scheduled Task After Another Task Completes?
next post
Configure PowerShell Remoting (WinRM) for Non-Domain (Workgroup) Computers

Related Reading

Configure User’s Folder Redirection with Group Policy

February 3, 2023

Disable Built-in PDF Viewer in Microsoft Edge

February 3, 2023

Join a Windows Computer to an Active Directory...

February 2, 2023

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Configure User’s Folder Redirection with Group Policy

    February 3, 2023
  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Configuring Port Forwarding in Windows
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top