Solved

Questions about move-item Powershell command

Posted on 2016-09-24
6
56 Views
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.


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 ?
0
Comment
Question by:donpick
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:Learnctx
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
{
    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.
0
 
LVL 68

Assisted Solution

by:Qlemo
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?
0
 

Author Comment

by:donpick
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?
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 17

Accepted Solution

by:
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.

http://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx

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 127.0.0.1) 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.
0
 

Author Comment

by:donpick
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.
0
 

Author Closing Comment

by:donpick
ID: 41829081
Thanks for the help
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now