Powershell - rename folder in C:\Windows\ - windows

I need some help. I need to rename a few folder in c:\Windows\ but it keeps saying that access is denied. I'm running poweshell as admin.
Rename-Item -path 'C:\Windows\SoftwareDistribution' -NewName 'C:\Windows\SoftwareDistribution.bak'
I get the return:
Rename-Item : Access to the path 'C:\Windows\SoftwareDistribution' is denied.
At line:1 char:2
+ Rename-Item -path 'C:\Windows\SoftwareDistribution' -NewName 'C:\Win ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Windows\SoftwareDistribution:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

I would guess that you are running this cmdlet as consent admin. (UAC)
You have to start your powershell console as administrator.
(It is not enough to be logged in as administrator.)

I had the exact same issue:
Rename-Item -Path PATH1 -NewName PATH2
gave me an IOException. I tried the older REN command but it is aliased to Rename-Item anyway and also did not work.
The only solution that worked for me was to rename it in File Explorer.

You'd need to close your file explorer and also the IDE file then you can use Rename-Item PATH1 -NewName PATH2

One possibility is simply a permission problem, which we can rule out, apparently.
The other likely explanation is that a process still holds an open handle on that folder, or a file within, most likely Windows Update. You'd have to stop the Windows Update service before you do that.

Try using the -Force switch as well, As long as the PowerShell Session you are running the cmdlet in there shouldn't be any issues.

I think the significant part of the error is where it says IOException:
+ CategoryInfo : WriteError: (C:\Windows\SoftwareDistribution:String) [Rename-Item], IOException
Check that the folder is not in use, just in case.

I've had the same problem before. You need to make sure the directory is not open in any command prompts and you need to turn of the wuauserv service.
You can also use Svish answer in this thread to look for folder locks in the Windows Resource Monitor.
https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows

Related

Setting Affinity of an Application on cmd

I have this problem when I'm using Voicemeeter and Discord together, my voice is just crackling and cutting out. I found out the solution to this problem. It's by going to the task manager, heading to details, right clicking audiodg.exe, and then setting it's affinity to only one processer. The problem is I don't want to do this all by hand everytime I start my computer. Is there any way I can write a line of code into the cmd that changes this? This way I can save this lane as a bat file and then put it into the shell:startup and everytime I turn my computer on it will do it automatically for me.
Thank you so much in advance.
Edit:
I'm sorry I wasn't aware of that. This is the error I get:
C:\Users\borah>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
"$Process = Get-Process audiodg.exe; $Process.ProcessorAffinity=1"
Get-Process : Cannot find a process with the name "audiodg.exe".
Verify the process name and call the cmdlet again. At line:1 char:12
$Process = Get-Process audiodg.exe; $Process.ProcessorAffinity=1
~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (audiodg.exe:String) [Get-Process], ProcessCommandException
FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
The property 'ProcessorAffinity' cannot be found on this object.
Verify that the property exists and can be set. At line:1 char:37
$Process = Get-Process audiodg.exe; $Process.ProcessorAffinity=1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : PropertyNotFound
You can set affinity by typing a command, like following,
Start /affinity 2 notepad
This will start notepad while setting an affinity to second core of CPU.
You can save the command above in a file ending with .cmd and run it by double clicking like any other application. Or you can use task scheduler to launch it with windows startup every time, or you can run it as a startup application.
Edit
If you meant changing affinity of an already running process. Check Mofi's comment as solution.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "$Process = Get-Process audiodg; $Process.ProcessorAffinity=1"

How to use PowerShell to remove nonempty folder in OneDrive?

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>

Symlinks cannot be created in Powershell 5.1 but can be created by Powershell 7 and Command Prompt

Windows Developer Mode is enabled, all of the following are non admin shells.
In all of the scenarios detailed below, a file named target exists in the current directory.
Via Powershell 5.1 the following gives an ERROR
PS C:\Users\user\repos\tmp> Get-Host | Select-Object Version
Version
5.1.19041.610
PS C:\Users\user\repos\tmp> New-Item -Path link -ItemType SymbolicLink -Target target
New-Item : Administrator privilege required for this operation.
At line:1 char:1
+ New-Item -Path link -ItemType SymbolicLink -Target target
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\user\repos\tmp\target:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemSymbolicLinkElevationRequired,Microsoft.PowerShell.Commands.NewItemCommand
Via Powershell 7.1 the following works
PS C:\Users\user\repos\tmp> Get-Host | Select-Object Version
Version
7.0.2
PS C:\Users\user\repos\tmp> New-Item -Path link -ItemType SymbolicLink -Target target
la--- 3/13/2021 1:55 AM 0 link -> target
Via both Powershell 5.1 and Powershell 7 the following works
PS C:\Users\user\repos\tmp> cmd /c mklink link .\target
symbolic link created for link <<===>> .\target
Via Command Prompt the following works
C:\Users\user\repos\tmp>mklink link target
symbolic link created for link <<===>> target
Does anyone have an idea why only Powershell 5.1 is giving this error, and is there a way to solve this?
When Microsoft enabled non-admin symbolic link creation (with developer mode enabled) they did it in a way that all programs not coded to use it can't use it, and updated mklink to be able to use it.
Obvious workaround: invoke mklink from Powershell via cmd /c, which you have already discovered.

Copy-Item shortcut fails to copy

I have a list of files I need to put on every new computer my company gets and I have automated it all with PowerShell, but I can't get this last part to work.
This is the command that is not working I'm wanting to put a shortcut in the start_Menu. What I have found out is that in order for myself to put a file in the location it requests administrator permission to copy. My account is an Admin so I just click continue and file will transfer. I want it automated.
Copy-Item G:\Work\BGInfo\updatebginfo.lnk C:\ProgramData\Microsoft\Windows\Start_Menu -Force
Also when I test the file location it comes back false even if I have manually put the shortcut at the location.
Test-Path C:\ProgramData\Microsoft\Windows\Start_Menu\updatebginfo.lnk
So after looking into more of the Developer code from Microsoft I found the answer. According to what I found was to add "" when any code in PowerShell needs to have a space.
Original code:
Copy-Item G:\Work\BGInfo\updatebginfo.lnk C:\ProgramData\Microsoft\Windows\Start_Menu -Force
Correct code:
Copy-Item "G:\Work\BGInfo\updatebginfo.lnk C:\ProgramData\Microsoft\Windows\Start Menu" -Force

Batch Rename at

I need help with a Command Prompt Script with regards to Bulk Renaming AVI & JPG files within their folders by using their respective "Folder Names", however the JPG file named "folder" must remain unchanged.
Example Before:
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984).avi
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984).jpg
C:\Temp\Videos\Terminator\Folder.jpg
Example After:
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984ST).avi
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984ST).jpg
C:\Temp\Videos\Terminator\Folder.jpg
Thanks in advance
Try with Windows PowerShell: see here how to open Windows PowerShell.
And try these commands line:
cd C:\Temp\Videos\"Terminator (1984ST)"
dir * | rename-item -NewName {$_.name -replace "1984","1984ST"}
Reference: PowerShell, commands

Resources