1

Ok, I'll try again. This got deleted before I had a chance to reply.

I'm new to Powershell and I've tried with no success to build a script to create a new folder, prompt the user to name the folder, then copy/move files into the new folder from an external drive. I have searched your site and the Net, but no luck. Any advice please? Thanks.

I've used the following lines to create a folder and copy files to it:

new-item "C:\Users\Dan\Pictures\DANTEST" -type directory
Copy-Item H:1*.jpg "C:\Users\Dan\Pictures\DANTEST"

But that, as you can see, has a pre-determined folder name in the script. I've only found a VB Script that brings up a box for user input, but nothing on how to take the input and apply it to a new folder, then copy the files to that new folder.

1
  • 2
    Look up Read-Host. Commented Jan 8, 2016 at 16:15

2 Answers 2

2

As vonPryz suggested, you can use Read-Host to prompt for the input of a folder name, then store it a variable which you can then include in your desired path.

Try:

 $folderName = Read-Host -Prompt 'Input folder name' 
 new-item "C:\Users\Dan\Pictures\$folderName" -type directory 
 Copy-Item H:1*.jpg"C:\Users\Dan\Pictures\$folderName"
Sign up to request clarification or add additional context in comments.

Comments

1

Try this, also read about creating Powershell Functions.

Create a script with the following at the top. For example Copy-Files.ps1

Param([Parameter(Mandatory=$true)][string]$directory)

Copy-Item H:1*.jpg "C:\Users\Dan\Pictures\$directory"

Now we run the script by doing:

PS C:\> .\Copy-Files MyFolder

1 Comment

Thanks guys. Gnarlywhale's answer did the trick. Going to try the second as well. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.