Powershell
--
Questions
--
Followers
Top Experts
The values of the directories are ok, the rights on the directories are ok. I added the code of the entire function.
The $oldDir and $newDir contain the right values at execution of the Move-Item cmdlet but the force command doesn't work.
Powershell isn't case-sensitive but I even tried to change the -Force to - force but as I expected that doesn't solve the problem.
This is the error I receive:
VERBOSE: Performing operation "Move Directory" on Target "Item: E:\TEST\Display\ZORG\63001
E:\TEST\Display\ZORG\630\6
Move-Item : Cannot create a file when that file already exists.
At E:\TEST\ImageConversion1.1
+             Move-Item  <<<< $oldDir $newDir -Force -Verbose
Function Reorganise-Folders($s, $d){
If($s.length -and $d.length -gt 0){
For($i = 0; $i -lt $s.length; $i++){
If((Check-Directories $s[$i]) -ne 0){ exit 0}
If((Check-Directories $d[$i]) -ne 0){ exit 0}
$arraySourcedirs = Get-ChildItem $s[$i] | Where { $_.attributes -eq "Directory" }
If($arraySourcedirs.length -gt 0){
Foreach($item in $arraySourcedirs){
# Converteren van arrayitem naar string
[string] $dir = $item
If($dir.length -gt 3){
$newName = $dir.Substring(0,3);
$oldDir = $s[$i] + "\" + $dir
$newDir = $d[$i] + "\" + $newName
If(!(Test-Path $newDir)){
# Als deze niet bestaat maak het nieuwe pad aan
New-Item $newDir -type directory
}
# THIS IS THE PROBLEM AREA
Move-Item $oldDir $newDir -Force
}
}
}
}
}
}
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I am not sure why you are breaking the folder down into seperate items , see if the following fiunction would for work for you.
function moveFolder
{
param(
[string] $from = $(throw "SourceFolder required."),
[string] $to = $(throw "Destination Folder required.")
)
move-item $From $to -force
}
# the following will move all files and folders from folder t1 create folder t2 and put them all in it
#########################################
# usage
#movefolder c:\test1\t1\ c:\test1\t2\
#########################################
movefolder c:\t1\ c:\test1\t1\
The thing I find strange is that the force command doesn't do anything because in  every article it should instruct Powershell to ignore the errors and overwrite the file or directory.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Powershell
--
Questions
--
Followers
Top Experts
Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.