0

My issue is if I used the move-item function it will delete the desktop folder, and I also don't want to move the 'recycling bin' app/shortcut as well as another custom file "logs.txt" Except for these 2 items, I want all the other folders, files, and shortcuts moved into the destination folder ("C:\Temp"). When I tried using move-item with the * (i.e. C:\Users\Username\Desktop*) to "move everything" but it only moved all the files and did not preserve the folders / folder structure. it just pulled all the files out of all the folders.

How do I move files and folders, while preserving folder structure/hierarchy, and excluding certain files (specifically the ones listed above) ?

the script will be used automatically to keep the user's desktop clean and tidy. the "Temp" folder is where unsorted files and directories should be store until the user properly sorts each one.

Here is what I have so far:

$dest = "C:\Temp\"; 
$currentPath = "$($env:USERPROFILE)\Desktop\"; 
try {
    Move-Item -Path $currentPath -Destination $dest
}
catch {
   ...
}

Also tried this: (got the same results)

$dest = "C:\Temp\"; 
$currentPath = "$($env:USERPROFILE)\Desktop\";  
try {
    Get-ChildItem -Path $currentPath -Recurse | Move-Item -Destination $dest
}
catch {
   ...
}

Do I need to break it up and use the copy-item and remove-item functions to add the files to the new folder, and then delete the old locations i.e. the original copies on the desktop?

1 Answer 1

0

From Move-Item online documentation Notes section:

This cmdlet will move files between drives that are supported by the same provider, but it will move directories only within the same drive.

Because a Move-Item command moves the properties, contents, and child items of an item, all moves are recursive by default.

So it's not clear which of your code samples gave you a flat directory (all files, but no sub-folders). The first one should not, but the second one would, because you're recursively getting the items, but they're piped to Move-Item as individual objects, all with a single destination.

  • To avoid moving the Desktop folder itself, you want to append \* to the source path. This selects all items within the folder but not the folder itself.
  • Use the -Exclude parameter to preserve "logs.txt" on the Desktop.
  • If you have multiple users on the same computer, you might want to consider creating the destinatino folder within the user profile.
$dest = "C:\Temp\"

# Querying the shell for the path of the known folder is more robust because it works when the folder has been re-directed:

$UserDesktopPath = (New-Object -Com shell.application).Namespace("Shell:ThisPCDesktopFolder").Self.Path
try {
    Move-Item -Path "$UserDesktopPath\*" -Destination $dest -Exclude 'logs.txt'
}
catch {
    # ...
}

Interestingly, while the -Excluse parameter works as intended, it also throws an error:

Move-Item : Cannot move item because the item at
'C:\Users\keith\DummyDesktop\logs.txt' does not exist.
At line:7 char:5
+     Move-Item -Path "$UserDesktopPath\*" -Destination $dest -Exclude  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperatio
   nException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveI
   temCommand

Display of this can be surpressed by adding the -ea silent paramaeter.


  • Remember, some items on the Desktop may come from c:\Users\Public\Public Desktop.
  • The Recycle Bin, along with This PC, Network, UsersFiles, and Control Panel, are not file system items, but virtual folders or namespace junctions. Whether or not these are shown is controlled by the Desktop Icons dialog accessed via Settings > Personalization > Themses.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .