Posted On December 3, 2020

PowerShell: How to Replace a System File – For Experimentation Purposes

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: How to Replace a System File – For Experimentation Purposes
# When attempting to rename a system protected file such as notepad.exe
$notepadExe='C:\Windows\system32\notepad.exe'
$newNotePadExe='C:\Users\rambo\Desktop\notepad.exe'
rename-item $notepadExe "$notepadExe.bak" -force

# Error message
rename-item : Access to the path is denied.
At line:1 char:1
+ rename-item $notepadExe "$notepadExe.bak"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\system32\notepad.exe:String) [Rename-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RenameItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RenameItemCommand

# Grant local admins full access to the system file
$notepadExe='C:\Windows\system32\notepad.exe'  
$fullAdminPermissions = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","Full","Allow")
$acl = Get-ACL $notepadExe
$acl.AddAccessRule($fullAdminPermissions)
Set-Acl $notepadExe $acl

# Rename the old notepad.exe and then copy the new one into its stead
rename-item $notepadExe "$notepadExe.bak" -force
copy-item $newNotePadExe 'C:\Windows\system32\notepad.exe'


# Result: when trying to open the new notepad.exe, this error occurred
---------------------------
notepad.exe - System Error
---------------------------
The program can't start because api-ms-win-shcore-path-l1-1-0.dll is missing from your computer. Try reinstalling the program to fix this problem. 
---------------------------
OK   
---------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Quick EMCopy

# Short Version $arr=@(); $arr+=[PSCustomObject]@{From='D:\Someshare';To='\\NEWSERVER\Someshare'} $arr+=[PSCustomObject]@{From='D:\Test';To='\\NEWSERVER\Test'} # Normal copying # $arr|%{emcopy $_.From $_.To /s /de…

PowerShell: How to Change Active Directory Username

$oldUsername='testUser' $newUsername='tUser' function changeUsername{ param( $oldUsername, $newUsername ) $domainName=$env:USERDNSDOMAIN $newUserPrincipleName="$newUsername@$domainName" try{ Set-ADUser $oldUsername -SamAccountName $newUsername…

PowerShell: Connecting via WinRM with a Timeout

$credentials=get-credential $sessionTimeout=New-PSSessionOption -OpenTimeout 120000 # 2 minutes $sessionIncludePort=New-PSSessionOption -IncludePortInSPN -OpenTimeout 120000 $session=if($credentials){ try{ New-PSSession -ComputerName…