Why is PowerShell copying to a random location? - powershell

I have the following simple script:
$workingDir = "C:\foo\bar"
$projectsDir = "C:\foo"
Copy-Item -Path "$projectsDir\some subpath\MyFile1.dll" -Destination $workingDir
Copy-Item -Path "$projectsDir\somewhere else\MyFile2.dll" -Destination $workingDir
Copy-Item -Path "$projectsDir\another place\MyFile3.dll" -Destination $workingDir
For some unknown reason, every time I run this script it copies the files to the correct location ($workingDir) and also copies them to $projectsDir\some subpath\something\else. I have to go delete the extra files from the other location every time this script is run.
So far I've tried:
changing variable names
specifying -Destination "$workingDir\MyFile1.dll"
using $null = Copy-Item -Path "...."
I even tried replacing Copy-Item with xcopy.exe
and nothing changes. I put a breakpoint on the first Copy-Item command and looked at the variables - they all looked right. What's going on here?

The only other thing I could think of is to run the copy-item like this:
Copy-Item -Path $($projectsDir + "\some subpath\MyFile1.dll") -Destination $workingDir
This is how I declare almost all of my Variable + SomethingElse scenarios. Since I haven't scene this behavior, I'll go back and test it some more to see what I can find. If I come up with something else, I redo my answer.

I rebooted my computer. Problem solved.

Related

Issue in Copy-Item in powershell

I used the Copy-Item command to copy a set reports to a server location. It was working fine before but recently more reports have been added to the folder and now most of the times it works fine but in some cases it shows the error:
The target file "$destination" is a directory, not a file
The code I used is:
Copy-Item -Path "$Source" -Destination "$destination" -Recurse -Force
I am not sure why I am not getting this error for every case.
What you have should work but this may work around the issue if there is some limitation with certain shared folder destinations:
Note: Remove the -WhatIf once you confirm the changes it will make are correct. Currently it will only output what files would have been copied.
Get-ChildItem $Source | Foreach-Object {
Copy-Item $_.FullName $destination -Recurse -Force -WhatIf
}
Basically, this enumerates all of the files and folders under the $Source directory, then copies the files or folders individually to the destination.
If you are able to offer the values of $Source and $Destination I or another might be able to offer a solution to your problem, rather than a workaround.

Copy-Item Issue using a variable as destination folder name

I am stuck with something weird you may help me.
I am doing the following:
$DateStringInput=Read-host
Copy-Item -Path "C:\Hodzic\PowerShell\Testordner\Q_Schulungspräsentationen\Vorlage" -Destination "C:\Hodzic\PowerShell\Testordner\Q_Schulungspräsentationen\$DateStringInput_KSF_Grundlagen" -Recurse
This doesnt work as the error says you cannot overwirte [Folder] with itself...the error repeats for each file contained in the copy-item path.
If I now put a space or anything else in front of "_" in the last part of the destination part it works:
...$DateStringInput _KSF_Grundlagen" or $DateStringInput+_KSF_Grundlagen"
Can you help?
The PowerShell parser interprets all of $DateStringInput_KSF_Grundlagen as a single variable expression.
Enclose the variable name in {} to prevent that:
$Destination = "C:\Hodzic\PowerShell\Testordner\Q_Schulungspräsentationen\${DateStringInput}_KSF_Grundlagen"
Copy-Item -Path "C:\Hodzic\PowerShell\Testordner\Q_Schulungspräsentationen\Vorlage" -Destination $Destination

Powershell move all files and subfolders in a child folder to CURRENT directory

I tried this, no error but no files have modved :
Get-ChildItem -Path "subfolder\" -Recurse | Move-Item -Destination "."
Update: I don't want ABSOLUTE but RELATIVE path
Your answer will be something like:
Move-Item -Path 'subfolder\*' -Destination . -Force
This is relative and will process all hidden files en folders as well.
Set-Location 'C:\Users\username\Desktop\newFolder';
Get-ChildItem -Path 'C:\Users\username\Desktop\oldFolder\*.txt' -Recurse -File | Move-Item -Destination $(get-location).Path;
This may seem like a crazy attempt and to be honest the question isn't as comprehensive as I'd like. For example: I'm not sure if the folder will only every be 1 level deep or could be further down from the current directory. At any rate, I got something that seems to be working basically by not bothering with recursion:
$SubFolder = 'YourFolderName'
$SubFolder = Get-Item .\$SubFolder
Get-ChildItem $SubFolder |
ForEach-Object{
$Destination = $_.FullName -Replace "$($SubFolder.FullName.Replace('\','\\'))", ".\"
$NewParent = $Destination -Replace $_.Name
Move-Item $_.FullName -Destination $NewParent
}
I think the trick here is you have to move to the parent's parent, etc or however many levels back/up. So figure out what the new parent should be using the current directory syntax .\. Once you have that you can move everything from the first level over not using recursion. Move-Item on a folder it knows to move the whole thing, and the files that are found in Sub-folder itself will obviously move to the new correct parent.
I had another working version that did use recursion, but this seems more concise and much to my surprise this seems to work. However, I'm not thrilled with it.

What am I doing wrong with Copy-Item?

Copy-Item trips me up every time I use it. Would someone please tell me what I did wrong here?
I have a set of folders:
C:\FolderA\Thing\Sub1\1_file.css
C:\FolderA\Thing\Sub2\A_file.css
C:\FolderA\Thing\page.html
I try to use Copy-Item to send them to C:\FolderB\* and preserve structure:
Copy-Item -Path $srcFolder\Thing\* -Destination $destFolder\Thing -Recurse -Force -Verbose
Expected result:
C:\FolderB\Thing\Sub1\1_file.css
C:\FolderB\Thing\Sub2\A_file.css
C:\FolderB\Thing\page.html
Instead I end up with:
C:\FolderB\Sub1\1_file.css
C:\FolderB\Sub2\A_file.css
C:\FolderB\page.html
There's clearly a \Thing folder in there, so why doesn't it create the \Thing if I'm telling it to go recursively?
You need to tell it to create the directory before the copy, for example:
$dst = C:\FolderB\Thing
New-item $dst -type directory
`

how to move bulk move files and then rename them in sequential order?

to test, I'm trying to move c:\Users\myuser\Downloads\test.txt to c:\Users\myuser\Documents\mynewname0001.txt.
The destination folder path is dynamic so for my test I'm using a variable
My final aim is to use GCI + ForEach-Object to move lots and lots of files. But I'm testing with a single file for now.
Here's my script:
$_test = "c:\Users\myuser\Documents"
$_i = 1
Move-Item -Path "$($_test)\test.txt" -Destination "($($_test)\temp\'newname{0:D4}' -f $_i++)"
This is the error I'm getting
Move-Item : Cannot find drive. A drive with the name '(c' does not exist.
I can move the files if I don't order them using:
Move-Item -Path "$($_test)\test.txt" -Destination "$($_test)\temp\newname.txt"
But I do not know how to introduce the auto incrementing code with it.
Can you please help? Thanks
Try to evaluate the format string separately:
Move-Item -Path "$($_test)\test.txt" -Destination "$_test\a\$('newname{0:D4}' -f $_i++)"