Avatar of donpick
donpick
 asked on

Questions about move-item Powershell command

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.


Questions:
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 ?
Powershell

Avatar of undefined
Last Comment
donpick

8/22/2022 - Mon
Aard Vark

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
{
    param
    (
        [String]
        [Parameter(Mandatory)]
        $Source,

        [String]
        [Parameter(Mandatory)]
        $Destination,

        [String]
        $Filter = '*',
        
        [Switch]
        $Recurse,
        
        [Switch]
        $Open
    )

    if ($Recurse)
    {
        $DoRecurse = '/S'
    }
    else
    {
        $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.
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
donpick

ASKER
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?
ASKER CERTIFIED SOLUTION
Aard Vark

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
donpick

ASKER
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
donpick

ASKER
Thanks for the help