We help IT Professionals succeed at work.

Move-Item Powershell

mikeydk
mikeydk asked
on
Hey

I would like to move the content of a folder into another folder using powershell.

The content of B into A

C:\Power\A
C:\Power\B\File1.txt (containing the files and folder)

To:

C:\Power\A\File1.txt

(even if A does not exists..)

How to?

Mike
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Something like this?
$src = 'C:\Power\B\file.txt'
$dst = 'C:\Power\A'
if (!(test-path $dst)) { md $dst }
move-item $src $dst

Open in new window

Author

Commented:
Qlemo...

If I use user example i get:
C:\Power\A\B\file.txt

It have to be C:\Power\A\file.txt

Mike
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
No, it will not copy the tree, only the file.
Commented:
$root = "E:\Homedir10"

$users = Get-ChildItem $root

$users | ForEach {robocopy /move $root\$_\Dokumenter $root\$_\Documents -R:0 -W:0}
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Your question has been asked clumsy. All you have to do in that case is to rename each user's folder Dokumenter in Documents. Your "solution" is a very costly, unnecessary operation. Just use
Get-ChildItem E:\Homedir10 |
  ? { $_.PsIsContainer } |
  Get-ChildItem -Filter "Dokumenter" | Rename-Item -NewName Documents

Open in new window