Questions about move-item Powershell command

Posted on 2016-09-24
Last Modified: 2016-10-04
Running Powershell v4 on Windows 7 pro.

I am a Powershell novice.  I want to move files from directory A to directory B.  If the file already exists in directory B then the file is not to be moved.

So the first thing I would do is to start Powershell and change to directory A

cd \”directory A”

Then execute :
  Get-Childitem -Path “.” -Recurse | move-item -Destination “directory B”

The “.” represents directory A.  Quotes surround “directory B” because the directory name may have a space in it.

1.)  Before I execute this command and possibly corrupt or destroy data, does the command look correct ?  What would you change?

2.) My understanding is parameters can be fed to a Powershell script.  What commands in Powershell would you use to feed the source and destination directories to a move-item command?

3.) Once the move-item command is put into a .ps1 file, how do I schedule this Powershell file?  May be you might have an example you could share?

4.)  My understanding is before a Powershell ps1 file can be executed some sort of permissions must be changed?  What must be done to set the permissions ?
Question by:donpick
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 17

Expert Comment

ID: 41814396
I mean, PowerShell can do this stuff, but you're better off using a built for purpose tool like Robocopy to do the job. Multi-threaded with a bunch of options. Someone has written a handy Robocopy function to make life easier for anyone who can't be bothered with the options (source).

#requires -Version 3

function Invoke-Robocopy


        $Filter = '*',

    if ($Recurse)
        $DoRecurse = '/S'
        $DoRecurse = ''

    robocopy $Source $Destination $Filter $DoRecurse /R:0 
  if ($Open)
      explorer.exe $Destination

Open in new window

Look to for the Robocopy switch options. PowerShell can do this, but I would say it is not the best tool for the job.
LVL 70

Assisted Solution

Qlemo earned 250 total points
ID: 41815107
Learnctx' comment lacks a lot of specific info, but I agree to better use RoboCopy.

1.) (Probably) not correct. You are moving files from a folder tree into a single folder, loosing the path info. That is usually unwanted. But if that is intended, you can always test move-item and most other cmdlets applying a change by adding -whatIf - this only will show what the performed action would look like, not do anything.

2.) Basically that has been answered by Learnctx, just change the code accordingly.

3.) You can call PowerShell scripts directly with recent OS' task scheduler. But it is better to expicitly call the shell with some more parameters:
powershell -ExecutionPolicy ByPass -NonInteractive -File C:\Scripts\YourFile.Ps1

Open in new window

The first parameter is because of 4.), PowerShell only allowing signed scripts by default for security reasons.

4.) You usually call Set-ExecutionPolicy RemoteSigned once in PowerShell to allow local scripts to be called without need to sign them (proving they are unchanged and trustworthy). Remote scripts, in particular those downloaded from Internet, still require a signature to run, which protects against malicious site downloads (not a severe thread yet, but might be some day).

Having said that all, RoboCopy is what I would use (as mentioned). But here are some questions we need answers for to provide something really suitable:
1. Do you want to move all files into a single target folder?
2. How is it possible to have the same file name in the source location again after having moved it?
3, Do you insist on using (native) PowerShell for this task?

Author Comment

ID: 41817096
Hello Qlemo:

Thank you for responding.

I know absolutely nothing about RoboCopy.  I don't know how useful it is.
I thought I would learn how to use Powershell because, from what I read, it is now the preferred scripting language to use.

My job has nothing to do with programming.  I have limited free time.  Since Powershell is supposedly the scripting language to use I thought it might be helpful in maintaining my small peer-to-peer 5 machine network.  This knowledge might be useful in my job someday.

I used to use fso and Vbscript  but it seems clumsy  especially when trying to move a directory and all the subdirectories and files.

Answering your questions:
1. Do you want to move all files into a single target folder?
 No.   I want files in directory A to go to directory B.  I also have files in directory C which need to be moved to directory D.
    Ideally I'd like to run the Powershell script and feed it the source and destination directories.  This was going to be the next question I would post to Experts Exchange once I understood more about how Powershell scripts worked and how to schedule them.

2. How is it possible to have the same file name in the source location again after having moved it?
 Currently I move files manually.  Sometimes a file is left in the source because of human error.  I could probably use Xcopy but I'd  like to keep up with technology and Powershell seems to be the scripting language to use (from what I have read).

3, Do you insist on using (native) PowerShell for this task?
  Well, I have limited time.  I know nothing about Robocopy.  How much does Robocopy cost?  I thought by learning Powershell I could learn one thing and may be master at least some of it.
   Perhaps you could suggest some Robocopy tutorial links ?  How would I feed parameters to Robocopy?
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

LVL 17

Accepted Solution

Learnctx earned 250 total points
ID: 41817188
Robocopy is a free tool from Microsoft. It comes as part of the OS usually. There is a great Wiki on the Microsoft page with use case examples of using Robocopy.

I don't think you'll find a better article than that. For a lot of smaller businesses, Robocopy is a poor man's DFS for syncing directories. It is just a command line tool, no scripting/coding is necessary if you don't want to use it. If you can use the ping command and understand switches (e.g. ping -t then you can use Robocopy.

An example would be:

ROBOCOPY C:\Temp\test\src\ C:\Temp\test\dst /e /move /xc /xn /xo /xd src /log:%temp%\robolog.txt

Open in new window

To explain the switches.

robocopy This is the command
c:\temp\test\src\ This is the source
c:\temp\test\dst This is the destination
/e Copy subdirectories including empty (/s excludes empty directories)
/move Move the files (copy then delete source)
/xc Exclude changed
/xn Exclude newer
/xo Exclude older
/xd src Excludes the source directory from deletion, if you wanted to

The /xc /xn /xo options will prevent you overwriting files in the destination which are different to the source.

Author Comment

ID: 41824317
Hello Learnctx

Thank you for the detailed answer.  That's what I need. I pay for this service so I really appreciate detailed answers containing useful links.

Author Closing Comment

ID: 41829081
Thanks for the help

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question