2

Is it possible run two commands at once such as

cd "C:/users/..." and Dir | -Rename-Item -NewName {...}

I want to know if this is possible since it will drastic speed up what I am trying to do.

2 Answers 2

3

From your description, it sounds like you are trying to run two commands sequentially. The way to do this is with a script. PowerShell uses .ps1 files for scripts. Running scripts in PowerShell is a little more complicated then scripts through the command line. You will need to enable running scripts, create the script, and finally run the script with the right command.

First you need to enable running scripts. Open PowerShell and run the command:

Set-ExecutionPolicy RemoteSigned

This will allow you to run scripts from your local computer.

Second, you need to write the script, You can use any tool you like, such as notepad, but if you want to get fancy, I recommend, PowerGUI script editor or Notepad++ so you can get proper syntax highlighting and such. In the case of your script, just copy the code onto two lines:

cd "C:/users/..." and 
Dir | -Rename-Item -NewName {...}

Finally you need to run the Script, this can be done in PowerGUI with the F5 key, or you can do it from the command line by browseing the the folder and typing:

.\Scriptname.ps1

The .\ is important, as this is how PowerShell knows that this is a script that it should run.

Powershell scripts can be a bit daunting at first, but you will find that with a bit of practice, you can do almost anything with them.

4

Use a semicolon to chain commands in powershell:

cd "C:/users/..."; Dir | -Rename-Item -NewName {...}

(from How do I run multiple commands on one line in Powershell?)

1
  • It is important to note that data does not pass the colon. It is essentially a way of separating two distinct commands. Also, if you are specifically working with those two commands, the cd command can be eliminated by using the appropriate relative paths, or the absolute paths in your second group of commands. See "Get-Help Get-ChildItem" for mode details on how to specify where dir/gci/get-childitem looks.
    – Jeter-work
    Aug 4, 2016 at 17:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .