Windows OS Hub
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux

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

March 17, 2024

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 the 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
PowerShellWindows 10Windows Server 2019
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

Wi-Fi (Internet) Disconnects After Sleep or Hibernation on...

March 15, 2024

Fix: Remote Desktop Licensing Mode is not Configured

August 24, 2023

How to Install Remote Server Administration Tools (RSAT)...

March 17, 2024

How to Find the Source of Account Lockouts...

March 12, 2024

How to Delete Old User Profiles in Windows

March 15, 2024

Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)

March 17, 2024

How to Backup and Restore Websites and IIS...

June 8, 2023

Slow Access to Shared Folders and Network Drives...

March 11, 2024

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Encrypt Any Client-Server App Traffic on Windows with Stunnel

    June 12, 2025
  • Failed to Open the Group Policy Object on a Computer

    June 2, 2025
  • Remote Desktop Printing with RD Easy Print Redirection

    June 2, 2025
  • Disable the Lock Screen Widgets in Windows 11

    May 26, 2025
  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • How to Delete Old User Profiles in Windows
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Configuring Port Forwarding in Windows
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
Footer Logo

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


Back To Top