I searched and found an icon, but it does not close programs that are running in subfolders.
I want to close programs running in subfolders
Note: You have to run the code in the folder you want, if anyone knows a way to add the file path instead of adding cd folder before the code, that would be fine.
This is the code
Get-Process | ?{$_.path -and (test-path (split-path $_.path -leaf ))} | Stop-Process -Force
Related
Descriptions
Cannot remove nonempty folder in OneDrive directory
Step to reproduce
Launch PowerShell in OneDrive directory
PS C:\Users\MyUserName\OneDrive>
Try to use Remove-Item cmdlet to remove a nonempty folder in this directory, for example: the .\test\ folder
PS C:\Users\MyUserName\OneDrive> Remove-Item .\test\
Expected result
Without the -Recurse parameter, PowerShell should return a confirm message, such as
Confirm
The item at C:\Users\MyUserName\OneDrive\test\ has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
Actual result
PowerShell return a error message
Remove-Item: Cannot remove item C:\Users\MyUserName\OneDrive\test\: The directory is not empty. : 'C:\Users\MyUserName\OneDrive\test\'
Note
PowerShell and Administrator:PowerShell get the same result;
If I exit OneDrive process and create a new nonempty folder under OneDrive directory, PowerShell can remove it as normal (see Note 4., because unsynced folders do not have the ReparsePoint attribute);
CMD can remove the folder successfully, which means I can use the below command in PowerShell to remove the folder too. But I want to accomplish my goal just by PowerShell cmdlet;
cmd.exe /C "rd /s test"
Get-ChildItem cmdlet shows that the mode of normal folders (not synced by OneDrive) is 'd'(directory), but the mode of synced folders is 'l'(reparsepoint). Is this the reason that I cannot remove a folder under OneDrive directory as normal?
Version info
PSVersion:7.1.3
OS:Microsoft Windows 10.0.19042
OneDrive Version:21.052.0314.0001 (Office 365 A1)
Update
I try to remove the test folder on PowerShell 5 but fail, too.
The error message from PowerShell 5.1:
PS C:\Users\MyUserName\OneDrive> Remove-Item .\test\ -Force -Recurse
Remove-Item : Access to the cloud file is denied.
At line:1 char:1
+ rm .\test\ -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Remove-Item], Win32Exception
+ FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand
I just hit the same thing and this worked for me:
Get-ChildItem -recurse .\test | Sort-Object -Property FullName -Descending | ForEach-Object { $_.Delete() }
(Get-Item test).Delete()
I'm rather new at PowerShell, so there might be more elegant or correct ways to do the above.
Previous answer did not handle hidden files,
You can add these to your profile
function rmc ($file) {
(Get-Item $file).Delete()
}
function rmd ($folder) {
Get-ChildItem -recurse -force $folder | Sort-Object -Property FullName -Descending | ForEach-Object { $_.Delete() }
(Get-Item $folder).Delete()
}
Onedrive folder's no different than any other folder in your system, the same folder removal PowerShell commands will be used. Here you go...
cd into the Onedrive folder.
Use, rm -r -fo <FolderName>
I am using dir \\pwdf1280\rep\"*.txt" /S to search for all the txt files in the folder rep. I used the /S argument to search for the same in the sub directories, but the problem is the command runs forever. I am basically writing a perl script which has to find these files along with their timestamp.
Is there any other approach to solve this, or can I improve on the above command?
The problem is the dir command I mentioned above runs forever and does not display files present in rep's sub directories. It just displays files present in rep directory. I want to search for a file in the rep's sub directories.
One way that might speed the process up is to run the directory search on the remote machine using PowerShell.
C:\src\powershell> type .\rc001.ps1
Invoke-Command `
-ComputerName 'pwdf1280' `
-Command { Get-ChildItem '\\pwdf1280\rep' -Recurse -Filter '*.txt' } |
ForEach-Object { $_.FullName }
Then, from a cmd script run:
C:\src\powershell> powershell -NoProfile -File .\rc001.ps1
Alternatives might be to run the dir command remotely using psexec from SysInternals or plink from PuTTY.
I have a very simple PowerShell script where I am copying data that's in one folder, and is putting it on an external hard drive. I would like to create a log file for failed events (meaning if some of the data was not copied) The data can be a .txt file. Here is the simple log file I have.
Set-ExecutionPolicy Unrestricted -Force
Copy-Item -Path C:\Program Files (x86)\Cummins Allisson\TempData\iFX1\* -Destination D:\backup -recurse
Set-ExecutionPolicy Unrestricted -Force
Remove-Item C:\Program Files (x86)\Cummins Allison\TempData\iFX1\* -recurse
I appreciate the help on this. I am very new to PowerShell and by no means an expert.
Thanks again
You could use the common parameters -ErrorAction and -ErrorVariable to assist you in this.
Copy-Item -Path C:\Program Files (x86)\Cummins Allisson\TempData\iFX1\* -Destination D:\backup -recurse -ErrorAction SilentlyContinue -ErrorVariable events
Remove-Item C:\Program Files (x86)\Cummins Allison\TempData\iFX1\* -recurse -ErrorAction SilentlyContinue -ErrorVariable events
Then you will have the variable $events which will contain all the details of the errors that occurred for both of those commands. SilentlyContinue surpresses the error output from the screen which, in addition to being stored in the default variable $error is stored in $events.
$events.Exception | Set-Content c:\temp\log.log
This would give you sample data like
An item with the specified name C:\temp\data2\data already exists.
An item with the specified name C:\temp\data2\data\folder1 already exists.
An item with the specified name C:\temp\data2\data\folder1\folder2 already
exists.
Which only happened since I didn't use the -Force parameter for Copy-Item which would have overwritten the files.
Side Note
I should think you would be able to use Move-Item for this code as well.
Depeding on how ofter this code is run in the same session you might need to set the variable $events to $null or equivalent so you don't get duplicate output.
I'm trying to run a script that copies several files to a group of servers. After words I run another script in another directory to verify they were moved. I can invoke the next folder that I need to go to using:
Invoke-Item c:\scripts\etc
But how can I get it to close a specific comObject? I can close all comObjects using:
(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}
but this will close all Windows, including IE if open. Is it possible to close it using a specific parameter like LocationName or LocationURL?
Thanks!
Figured it out..
(New-Object -comObject Shell.Application).Windows() | where-object {$_.LocationURL -eq "File:///C:/Scripts/Whatever/Folder/Path"} | foreach-object {$_.quit()}
In case anyone else is dumb like me :)
I use cygwin version of gvim to edit files in windows, for that I created a bat script that opens a file with cygwin version of gvim(by converting the path to cygwin format). I also wrote a small powershell script to register this bat script with windows explorer so I can associate file extensions using the 'open with' context menu. Here is the script:
$ErrorActionPreference = "Stop"
$classes="hkcu:\software\classes"
$appid="cygwin.gvim"
$apps="$classes\applications"
$cmd='...SOMEDIRECTORY...\edit-gvim.bat'
$friendlyname='gVim (Cygwin)'
$icon='...ANOTHERDIRECTORY...\vim.ico'
$filetypes=#(".txt", ".ps1", ".sh", ".py", ".cs", ".cpp", ".c", ".rb",
".zsh", ".bash", ".vbox", ".xml", ".yml", ".yaml", ".bat")
if (test-path "$apps\$appid") {
# cleanup
remove-item -recurse "$apps\$appid"
}
# register open commands to know filetypes
new-item -path "$apps\$appid\shell\open\command" -value "$cmd `"%1`"" -force
# add a context menu item(edit with gVim) to every file in windows explorer
new-item -path "$classes\*\shell\Edit with $friendlyname\command" -value "$cmd `"%1`"" -force
# friendly name for the 'open with' dialog
new-itemproperty -path "$apps\$appid\shell\open" -name 'FriendlyAppName' -value $friendlyname
# register the icon
# FIXME this has no effect
new-item -path "$apps\$appid\DefaultIcon" -value $icon -type expandstring
# register supported file extensions
new-item -path "$apps\$appid\SupportedTypes"
foreach ($ext in $filetypes) {
new-itemproperty -path "$apps\$appid\SupportedTypes" -name $ext -PropertyType string -value ''
}
Everything works except the line below the 'FIXME' comment, which aparently has no effect. Instead of seeing my provided icon with applications associated with gvim, I see windows default icon for unknown file types. What am I missing here?
Here are some resources I used to write this script:
http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/cc144158(VS.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/cc144101(v=vs.85).aspx
Create registry entry to associate file extension with application in C++
I can make a mistake, but as far as I understand the syntax of DefaultIcon is :
"Full path to a file,ordinal"
This is usefull when you want to point to a resource in an EXE or a DLL
C:\Program Files (x86)\PowerGUI\ScriptEditor.exe,1
But you have to keep the same syntax if you want to point to an icon file :
[HKEY_CLASSES_ROOT\AcroExch.acrobatsecuritysettings.1\DefaultIcon]
#="C:\\Windows\\Installer\\{AC76BA86-7AD7-1036-7B44-A95000000001}\\PDFFile_8.ico,0"
So, in your case, I would try :
$icon='...ANOTHERDIRECTORY...\vim.ico,0'
or
$icon='...ANOTHERDIRECTORY...\\vim.ico,0'
Edited
Don't forget to restart explroer.exe to see the new icon.