[Webinar] Streamline your web hosting managementRegister Today


Questions about move-item Powershell command

Posted on 2016-09-24
Medium Priority
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
  • 3
  • 2
LVL 18

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 https://technet.microsoft.com/en-us/library/cc733145%28v=ws.11%29.aspx?f=255&MSPPError=-2147217396 for the Robocopy switch options. PowerShell can do this, but I would say it is not the best tool for the job.
LVL 72

Assisted Solution

Qlemo earned 1000 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?
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

LVL 18

Accepted Solution

Learnctx earned 1000 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

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
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…
Loops Section Overview

607 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