Solved

Questions about move-item Powershell command

Posted on 2016-09-24
6
44 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 16

Expert Comment

by:Learnctx
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Accepted Solution

by:
Learnctx earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

8 Experts available now in Live!

Get 1:1 Help Now