Copy Selected Files to another folder via the Context Menu - arrays

I'm trying to copy selected items in a folder and paste them into a different folder via a script shortcut in the context menu.
This script is a shortcut to Powershell.exe that calls the .ps1 to execute. Technically, the script works, but it's only copying the file that is actually right-clicked instead of the group of selected files. In the screenshot, if I selected both "Saved Pictures" and "Screenshots", but right-clicked "Saved Pictures", it would only copy "Saved Pictures" even though "Screenshots" is also selected.
Function Collection{
#Selects the Item's Current Path
param($SourceFile)
#Copy the selected file to the Document Collection folder
Copy-Item $SourceFile -Destination (New-Item "$Env:UserName\My Documents\temp" -Type Directory -Force) -Recurse -Force
}
Collection $args[0]
I guess my main question for you guys is How do I copy all selected files instead of just the file that is clicked?

Hit the Windows and R key to open the Run dialog, type this and hit Enter :
shell:sendto
You can add a shortcut to your destination folder in the opened system folder to add it to the Send to list
This seems to fit what you want to achieve here.
Source : howtogeek

Related

Tricentis Tosca - How to open a file which is the first file in a folder, or the only file in a folder. Note - I dont know the file name

The System Under Test generates some pdf files and saves the files to a particular folder. The challenge is that the pdf file name is randomly generated and I don't know the name. I need to open the file from the folder which is the most recent one, but I can't seem to find a way to open the file.
I know part of the file name so I tried using wildcard for part of the file name, but it doesn't work
I removed all other files from the folder and kept only one file in the folder, and then used wildcard instead of the complete file name, but that doesn't work either
Is there a way that I can read names of files present in a folder? Or open a pdf file using wildcard character? Or open the first file in the folder?
You could try PowerShell scripting (please refer attached screenshots)
Use the 'TBox Start Program' module.
In the Path attribute, mention the complete path of your PowerShell exe file (Eg: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)
In the Directory attribute, mention the folder path(Eg: C:\Users\tosca\Documents\testfolder)
Put -command in arguments with ActionMode 'Select'.
As Argument, input the PowerShell script with your wild carded file name to copy the file name (Eg:
(Get-ChildItem filename | Sort-Object CreationTime -Descending | Select-Object -First 1).Name | Clip)
Copy latest file name
Then use 'TBox Clipboard' to store the dynamic file name using ActionMode 'Verify'
Save latest file name
Then you can buffer the target file as folderpath\filename.

Batch file to create a zip of several files and copy it somewhere else

I want to back up important files to my USB drive every day. The USB drive is always plugged into the computer, so I don't need to worry about drive letters. I know how to create a batch file to simply copy and paste them into the drive, but I was wondering if there is a way to create a batch file that makes a zip file of all the folders I want (using winzip or winrar), and then has them sent to the drive. That way I can archive them instead of just copying and replacing them.
Thank you.
See:How can you zip or unzip from the command prompt using ONLY Windows' built-in capabilities?
Powershell can do this:
Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('C:\foo\', 'D:\foo.zip')
you can incorporate it into a batch file by calling powershell.exe like so:
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('C:\foo\', 'D:\foo.zip'); }"
I would recommend running it from a scheduled task so you don't have to start it manually or worry about it running all the time, and plan on disconnecting/replacing/archiving the drive itself occasionally - locally attached storage is not a backup
Start WinRAR, click in menu Help on first menu item Help topics, expand on help tab Contents the list item Command line mode and
read the help page Command line syntax,
expand the list item Commands and read the help page Alphabetic commands list and
expand the list item Switches and read the help page Alphabetic switches list.
Then you know how I created this single command line below and what all those switches mean.
"%ProgramFiles%\WinRAR\WinRAR.exe" a -ac -ao -afzip -agYYYY-MM-DD_NN -cfg- -ed -ep1 -ibck -inul -m5 -r -y -- "D:\Backup Folder\DataBackup_.zip" "C:\Path To Folder With Files To Backup\"
See also answer on Simply compress 1 folder in batch with WinRAR command line?
WinRAR adds to ZIP archive only files with archive attribute set because of switch -ao and clears the archive attribute after compressing the file into the archive because of -ac. The archive attribute is automatically set by Windows when a file is modified, renamed or moved. So this command line avoids compressing the same unmodified files again and again into ZIP archives.
The ZIP file is named automatically with current date in name and an automatic incremented number in case of this command line is executed multiple times on one day.

Replacing files in a *.zip file without unzipping (extracting) in Powershell

Hi everyone!
Currently, I am trying to create a script in Powershell that will replace/delete files in a *.zip file without extracting it.
I know it is possible to do manually by using applications like 7Zip and WinRAR or by simply opening a zip archive in Folder Explorer, but I am looking for a way to automate this action.
I am new to powershell and to programming in general, so I cannot come up with any preliminary code. I tried to search for answers on the net, but failed to find anything that would work for me.
So the question is:
Lets say we have a zip archive called Photos (Photos.zip). The archive contains 5 images. How do I replace/delete images in Photos.zip without extracting it in Powershell?
Any code/ideas/links to helpful resources would be highly appreciated.
I used the following solution.
First I moved old files from the archive to a temporary folder by using this code:
#Path to the temporary folder
$pathToTemporaryFolder = "C:\...\Temporary"
#Path to a file that will be moved from the archive to the temporary folder
$pathToFileToBeMoved = "C:\...\Photos.zip\My Photos\My Image.png"
(New-Object -COM Shell.Application).NameSpace("$pathToTemporaryFolder").MoveHere("$pathToFileToBeMoved")
Then, I copied some new files to the archive:
#Path to the folder with a new file
$pathToFolderWithNewFile = "C:\...\New Images\My New Image.jpeg"
#Path to a folder in the archive where the new file will be copied to
$pathToFolderInArchive = "C:\...\Photos.zip\My Photos"
(New-Object -COM Shell.Application).NameSpace("$pathToFolderInArchive").CopyHere("$pathToFolderWithNewFile")
Finally I deleted the temporary folder
Remove-Item -Path "C:\...\Temporary" -Recurse
This is how I deleted files from the archive and copied new files to it without unzipping/extracting.

How to keep the shell window open after running a PowerShell script?

I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line.
How can I sort this out?
You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.
One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
Per-script Fix: Add a prompt for input to the end of your script file. e.g. Read-Host -Prompt "Press Enter to exit"
Global Fix: Change your registry key by adding the -NoExit switch to always leave the PowerShell Console window open after the script finishes running.
Registry Key: HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command
Description: Key used when you right-click a .ps1 file and choose Open With -> Windows PowerShell.
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""
Registry Key: HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command
Description: Key used when you right-click a .ps1 file and choose Run with PowerShell (shows up depending on which Windows OS and Updates you have installed).
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \"%1\""
See my blog for more information and a script to download that will make the registry change for you.
Errr...
I should have known:
powershell -noexit <path\script>
and that's all there's to it :)
The solution below prevents the script from closing when running Powershell ISE and allows the script to close otherwise.
# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
{
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
Just add pause on a new line at the bottom of the script, as in a batch file.
In my own case, I wanted to add powershell to context menu on windows 7. That is right clicking on a folder or inside a folder to get a menu to launch Powershell window without it closing after launch. The answers here helped me do just that and I want to share it here incase it helps someone else.
Launch registry editor by pressing WIN + R
type regedit.exe and hit enter
Navigate to HKEY_CLASSES_ROOT\Directory\Background\shell
Right click on shell and create a key give it a name e.g PowershellHere
On the right pane, double click on Default and provide a descriptive name e.g Powershell Here
Right click on the PowershellHere key you created earlier and create a new key and name it "command" please make sure you name it exactly so but without the quotes.
On the right pane, double click on Default and then type the command below
C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -Command CD '"%1"'
-noexit flag makes sure that the Powershell windows does not close again immediately after launch
'"%1"' flag represents the folder you right clicked
-Command CD '"%1"' will ensure the Powershell changes into the right clicked directory.
To make the right click work inside a folder meaning right clicking an empty space inside a folder, repeat the steps but this time, the registry location is:
HKEY_CLASSES_ROOT\Directory\shell
And the command is:
C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit
Tested on windows 7 ultimate sp1 but I think I might work for later versions of Windows as well
You can add pause at the end of your script if you just want the window to stay open, or you can add powershell if you want to be able to run commands afterwards (obviously don't do the second option if anyone else will use your code).

Delete specific folders in many folders and subfolders

So I've noticed that Windows creates hidden folders called "blabla.jpg.files" if you have enabled the thumbnail view in your picture folder. I sync my picture folder to my phone and NAS and would like to remove those hidden folders from there with powershell.
Since my picture folder also has subfolders which also have subfolders and so on, I would like to create a little loop.
The script should just scan in all subfolders of one folder for hidden folders with the name "*.files" and remove them.
Can somebody help me with this one.
You don't need to create loop as Get-ChildItem can search recursively for the specified item e.g.:
Get-ChildItem <path> -r -attributes h+d *.files | Remove-Item -r -whatif
The above command requires features that are new in PowerShell v3. If the list of folders to be deleted looks correct, remove the -whatif to actually delete them.
BTW I don't see that Windows creates these folders on my system. It does create a hidden file called Thumbs.db. Perhaps it is some other program that is creating these hidden folders?

Resources