In this article, we will learn how to move documents with exclusions in a subfolder using PowerShell. This is useful if you need to organize your files in a specific way, while excluding certain files or folders. This can be done using the Move-Item cmdlet in PowerShell, which allows you to move files and folders from one location to another.
Prerequisites
Before we begin, let's make sure you have the following:
- A computer with PowerShell installed
- A folder with documents that you want to move
- A subfolder where you want to move the documents
Creating the PowerShell Script
Now, let's create a PowerShell script that will move the documents with exclusions in the subfolder. To do this, follow these steps:
- Open PowerShell
- Create a new script by typing
New-Item -ItemType File -Path "C:\Scripts\Move-Documents.ps1" - Open the script in Notepad by typing
notepad "C:\Scripts\Move-Documents.ps1" - Add the following code to the script:
$source = "C:\SourceFolder" $destination = "C:\DestinationFolder\Subfolder" $exclusions = "excludedfile.txt", "excludedfolder" Get-ChildItem -Path $source -Recurse | Where-Object {$exclusions -notcontains $_.Name} | Move-Item -Destination $destinationThis code does the following:
$sourceis the path to the source folder where the documents are currently located.$destinationis the path to the destination folder where you want to move the documents.$exclusionsis an array of files and folders that you want to exclude from the move operation.- The
Get-ChildItemcmdlet is used to get all the files and folders in the source folder. - The
Where-Objectcmdlet is used to filter out the files and folders that match the exclusions. - The
Move-Itemcmdlet is used to move the remaining files and folders to the destination folder.
Make sure to replace the source, destination, and exclusions with your own values.
- Save and close the script
- Test the script by running
.\Move-Documents.ps1
In this article, we learned how to move documents with exclusions in a subfolder using PowerShell. We created a PowerShell script that uses the Move-Item cmdlet to move the documents, while excluding certain files and folders. This can be useful when organizing your files and folders in a specific way.
References
| Reference | Description |
|---|---|
| Move-Item | The Move-Item cmdlet moves an item from one location to another. |
| Get-ChildItem | The Get-ChildItem cmdlet gets the items in a directory. |
| Where-Object | The Where-Object cmdlet filters objects based on a specified condition. |