Powershell restore previous version of the files - powershell

We got hit by virus, it changed all common file extension to .kmybamf (.txt >> .txt.kmybamf ) and if I delete .kmybamf , the file got damaged.....
So I made a list of all files that got damaged. now I'm trying to overwrite them by previous version. Anyone knows how to do it in Powershell?
I can do it in cmd similar to this
subst X: \localhost\D$\#GMT-2011.09.20-06.00.04_Data
robocopy Z: D:\Folder\ /E /COPYALL
But I want to do it in one shot in Powershell, It has to be a "if .kmybamf found, then restore previous version." and powershell seems like has no such cmdlet for restoring previous version of files or folders.

$fileList = Get-Content -Path "\\localhost\D$\#GMT-2011.09.20-06.00.04_Data"
$destinationFolder = "D:\Folder\"
foreach ($file in $fileList)
{
Copy-Item -Path $file -Destination $destinationFolder -Force
}
This will also work but I find it less readable
Get-Content -Path "\\localhost\D$\#GMT-2011.09.20-06.00.04_Data" | ForEach-Object { Copy-Item -Path $_ -Destination "D:\Folder" -Force }

Get-Content is for reading the text from the files, to read the files from a folder you would have to use Get-Childitem

Related

[Powershell]: 2nd Execution of Copy-Item creates subfolder (5.1.17763.1007)

After searching for a wile I need to post my question here:
I want to do a simple task:
copy-item -path "C:\Folder Copied" -destination "C:\Folder Copied_New" -recurse
Assuming the dir "Folder Copied_New" doenst exist in "C:", PS will create a folder, and copy the folder (and its content) "C:\Folder Copied" to "Folder Copied_New"
HOWEVER, if you execute the command a second time, the following happens:
Powershell created: "C:\Folder Copied_New\Folder Copied" (content "Test.txt" was also copied to this newly created folder...
The 3rd time you execute the command, it ll say that the folder already exists...
So my question:
After I run the command a 2nd time, PS should throw an error, that "Folder Copied_New". How do I do that?
I tried copying and renaming the new folder, using and NOT using "" in the paths, but nothing worked. I do think of using -Testpath, but I thought I ask the community for a simplier (BestPractice) approache.
Thanks in advance for reading and advising!
Well explained in another question but same issue
Not an answer but to long to write in comments.
Can you run following script and show us the output?
$root = "$($env:TEMP)\test"
$source = "$($root)\Logfiles"
$destination = "$($root)\Drawings\Logs"
# Verify folders don't exist yet
if (Test-Path $source) {Throw}
if (Test-Path $destination) {Throw}
# Set up
New-Item $source, $destination -ItemType Directory -Force | Out-Null # Create folders
New-Item "$($source)\testfile" -ItemType File -Force | Out-Null # Create a testfile
# Show files/folders before copy
Write-Output "Before copy"
Get-ChildItem $root -Recurse | select fullname
# Copy first time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After first time copy"
Get-ChildItem $root -Recurse | select fullname
# Copy second time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After second time copy"
Get-ChildItem $root -Recurse | select fullname
# Copy third time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After third time copy"
Get-ChildItem $root -Recurse | select fullname
# Clean up
Remove-Item $source, $destination -Force -Recurse | Out-Null
On my computer, I don't observe any abnormal behavior. The source folder get's copied to destination after the first time. The second run does not alter anything, neither does the third run.
That behavior is the default:
Let's say you want to copy and rename an item, so you are doing this:
Copy-Item -path source.txt -Destination destination.txt
You expect a file named destination.txt with the content of source.txt.
If you want to copy a file but without renaming it you do this:
Copy-Item -path source.txt -Destination C:\test
You expect the file source.txt to apear in C:\test. If C:\test didn't exist it will be created.
Now let's try to copy a folder while the destination does not exist.
Copy-Item -path C:\test -Destination D:\toast
The destination didn't exist so you created it by copying the source folder to the destination. The content of the destination will be the same as the source. The folder D:\toast is the same as C:\test but it got renamed in the process.
However if you provide a destination path where the source object (folder) is going to be located, it will be located in there.
Copy-Item -path C:\test -Destination D:\toast
D:\toast did exist from our previouse action so a copy of C:\test will be created in there: D:\toast\test

Moving or copying several files at once in Powershell

In bash, you can
cp filea fileb filec Folder
which will copy filea fileb and filec into Folder
For some reason, i couldn't find a way to do the same thing in Powershell.
Everywhere i looked, all i could find is just the possibility to use wildcards in order to move
a bunch of files from the same type like:
cp *.txt Folder
But that's not what i'm looking for. I'm looking for a way to copy several files
by their names in one command.
Does anybody knows a way to do that?
"I'm looking for a way to copy several files
by their names in one command."
Here are three examples that can do what you want. The first one is closest to what you're looking for.
param(
$files = #('filea', 'fileb', 'filec'),
$dest = 'Folder'
)
Copy-Item -LiteralPath $files -Destination $dest -Verbose
$files | ForEach-Object { Copy-Item -Path $_ -Destination $dest -Verbose }
foreach ($file in $files) { Copy-Item -Path $file -Destination $dest -Verbose }
A one liner to do:
#("file1","file2","filen") | % {Copy -Path $_ -Destination "destination"}

Check if file exists, then move it

I'm trying to write a few lines of code in powershell, to check if a file arrived to a specific folder. If the file is there, copy it to another folder. No action required if the file is not there. So far I have only the copying part:
cd C:\
Move /y "C:\myfolder\*.csv" "C:\MyDestinationFolder"
I can't find a simple code to check if the file is present.
Maybe you can use this:
$SourceFile = "C:\source\file.txt"
$Destination = "C:\destination\"
if(Test-Path -Path $SourceFile)
{
Copy-Item -Path $SourceFile -Destination $Destination
}
try this :
move-Item "C:\myfolder\*.csv" "C:\MyDestinationFolder" -Force -ErrorAction SilentlyContinue

PowerShell to copy files to destination's subfolders while excluding certain folders in the destination

So I have danced with this off and on throughout the day and the timeless phrase "There's more than one way to skin a cat" keeps coming to mind so I decided to take to the community.
Scenario:
Source folder "C:\Updates" has 100 files of various extensions. All need to be copied to the sub-folders only of "C:\Prod\" overwriting any duplicates that it may find.
The Caveats:
The sub-folder names (destinations) in "C:\Prod" are quite dynamic and change frequently.
A naming convention is used to determine which sub-folders in the destination need to be excluded when the source files are being copied (to retain the original versions). For ease of explanation lets say any folder names starting with "!stop" should be excluded from the copy process. (!stop* if wildcards considered)
So, here I am wanting the input of those greater than I to tackle this in PS if I'm lucky. I've tinkered with Copy-Item and xcopy today so I'm excited to hear other's input.
Thanks!
-Chris
Give this a shot:
Get-ChildItem -Path C:\Prod -Exclude !stop* -Directory `
| ForEach-Object { Copy-Item -Path C:\Updates\* -Destination $_ -Force }
This grabs each folder (the -Directory switch ensures we only grab folders) in C:\Prod that does not match the filter and pipes it to the ForEach-Object command where we are running the Copy-Item command to copy the files to the directory.
The -Directory switch is not available in every version of PowerShell; I do not know which version it was introduced in off the top of my head. If you have an older version of PowerShell that does not support -Directory then you can use this script:
Get-ChildItem -Path C:\Prod -Exclude !stop* `
| Where-Object { $_.PSIsContainer } `
| ForEach-Object { Copy-Item -Path C:\Updates\* -Destination $_ -Force }
To select only sub folders which do not begin with "!stop" do this
$Source = "C:\Updates\*"
$Dest = "C:\Prod"
$Stop = "^!stop"
$Destinations = GCI -Path $Dest |?{$_.PSIsContainer -and $_.Name -notmatch $Stop }
ForEach ($Destination in $Destinations) {
Copy-Item -Path $Source -Destination $Destination.FullName -Force
}
Edited Now copies all files from Update to subs of Source not beginning with "!stop" The -whatif switch shows what would happen, to arm the script remove the -whatif.
Edit2 Streamlined the script. If also Sub/sub-folders of C:\Prod shall receive copies include a -rec option to the gci just in front of he pipe.

Need a script to publish build output to a staging server

I am trying to write a PowerShell script that will copy a subset of files from a source folder and place them into a target folder. I've been playing with "copy-item" and "remove-item" for half a day and cannot get the desired or consistent results.
For example, when I run the following cmdlet multiple times, the files end up in different locations?!?!:
copy-item -Path $sourcePath -Destination $destinationPath -Include *.dll -Container -Force -Recurse
I've been trying every combination of options and commands I can think of but can't find the right solution. Since I'm sure that I'm not doing anything atypical, I'm hoping someone can ease my pain and provide me with the proper syntax to use.
The source folder will contain a large number of files with various extensions. For example, all of the following are possible:
.dll
.dll.config
.exe
.exe.config
.lastcodeanalysisissucceeded
.pdb
.Test.dll
.vshost.exe
.xml
and so on
The script needs to only copy .exe, .dll and .exe.config files excluding any .test.dll and .vshost.exe files. I also need the script to create the target folders if they don't already exist.
Any help getting me going is appreciated.
try:
$source = "C:\a\*"
$dest = "C:\b"
dir $source -include *.exe,*.dll,*.exe.config -exclude *.test.dll,*.vshost.exe -Recurse |
% {
$sp = $_.fullName.replace($sourcePath.replace('\*',''), $destPath)
if (!(Test-Path -path (split-path $sp)))
{
New-Item (split-path $sp) -Type Directory
}
copy-item $_.fullname $sp -force
}
As long as the files are in one directory, the following should work fine. It might be a bit more verbose than needed, but it should be a good starting point.
$sourcePath = "c:\sourcePath"
$destPath = "c:\destPath"
$items = Get-ChildItem $sourcePath | Where-Object {($_.FullName -like "*.exe") -or ($_.FullName -like "*.exe.config") -or ($_.FullName -like "*.dll")}
$items | % {
Copy-Item $_.Fullname ($_.FullName.Replace($sourcePath,$destPath))
}