Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • 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 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

June 8, 2023 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

How to Use Ansible to Manage Windows Machines

September 25, 2023

Installing Language Pack in Windows 10/11 with PowerShell

September 15, 2023

Configure Email Forwarding for Mailbox on Exchange Server/Microsoft...

September 14, 2023

How to View and Change BIOS (UEFI) Settings...

September 13, 2023

How to Create UEFI Bootable USB Drive to...

September 11, 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

  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023
  • How to Create UEFI Bootable USB Drive to Install Windows

    September 11, 2023
  • Redirect HTTP to HTTPS in IIS (Windows Server)

    September 7, 2023
  • Add an Additional Domain Controller to an Existing AD Domain

    September 6, 2023
  • How to Install an SSL Certificate on IIS (Windows Server)

    September 5, 2023
  • Managing Windows Firewall Rules with PowerShell

    August 31, 2023
  • Fixing ‘The Network Path Was Not Found’ 0x80070035 Error Code on Windows

    August 30, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • How to Delete Old User Profiles in Windows
  • Get-ADUser: Find Active Directory User Info with PowerShell
Footer Logo

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


Back To Top