Remove-RunOnceRegKey.ps1
19 Sep 2025Description
Purpose
Removes a specified registry key from the RunOnce registry path.
Detailed Description
This function removes a specified registry key from the “HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce” path on the local or remote computers. It can accept computer names and credentials as input and uses PowerShell remoting to execute the task on remote systems.
Usage
Example 1
Remove-RunOnceRegKey -KeyName "MyRunOnceKey"
Removes the “MyRunOnceKey” from the RunOnce path on the local computer.
Example 2
Remove-RunOnceRegKey -ComputerName "Server01" -Credential (Get-Credential) -KeyName "MyRunOnceKey"
Removes the “MyRunOnceKey” from the RunOnce path on the remote computer “Server01” using the provided credentials.
Notes
Author: Luke Leigh Date: [Today’s Date]
Script
function Remove-RunOnceRegKey {
<#
.SYNOPSIS
Removes a specified registry key from the RunOnce registry path.
.DESCRIPTION
This function removes a specified registry key from the "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce" path on the local or remote computers.
It can accept computer names and credentials as input and uses PowerShell remoting to execute the task on remote systems.
.PARAMETER ComputerName
The name of the computer(s) to perform the action on. Defaults to the local computer if not specified.
.PARAMETER Credential
The credential to use for remote connections.
.PARAMETER KeyName
The name of the registry key to remove from the RunOnce path.
.EXAMPLE
Remove-RunOnceRegKey -KeyName "MyRunOnceKey"
Removes the "MyRunOnceKey" from the RunOnce path on the local computer.
.EXAMPLE
Remove-RunOnceRegKey -ComputerName "Server01" -Credential (Get-Credential) -KeyName "MyRunOnceKey"
Removes the "MyRunOnceKey" from the RunOnce path on the remote computer "Server01" using the provided credentials.
.NOTES
Author: Luke Leigh
Date: [Today's Date]
#>
[CmdletBinding(DefaultParameterSetName = 'Default',
SupportsShouldProcess = $true,
HelpUri = 'https://github.com/BanterBoy'
)]
[OutputType([string])]
param
(
[Parameter(ParameterSetName = 'Default',
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Enter computer name or pipe input'
)]
[Alias('cn')]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(ParameterSetName = 'Default',
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Enter credential for remote connections'
)]
[Alias('cred')]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[Parameter(Mandatory = $true,
HelpMessage = 'Enter the name of the registry key to remove'
)]
[string]$KeyName
)
# ScriptBlock to execute on remote computers
$scriptBlock = {
param($KeyName)
$path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
try {
Write-Verbose "Attempting to get the item property for key: $KeyName"
Get-ItemProperty -Path $path -Name $KeyName -ErrorAction Stop
Write-Verbose "Removing item property for key: $KeyName"
Remove-ItemProperty -Path $path -Name $KeyName -ErrorAction Stop
Write-Output "Key $KeyName removed successfully."
}
catch {
Write-Warning "Key $KeyName does not exist or could not be removed. Error: $_"
}
}
foreach ($Computer in $ComputerName) {
if ($PSCmdlet.ShouldProcess($Computer, "Remove RunOnce key $KeyName")) {
Write-Verbose "Processing computer: $Computer"
if ($Credential) {
Write-Verbose "Using credentials for remote connection."
Invoke-Command -ComputerName $Computer -Credential $Credential -ScriptBlock $scriptBlock -ArgumentList $KeyName -Verbose
}
else {
Write-Verbose "Connecting without credentials."
Invoke-Command -ComputerName $Computer -ScriptBlock $scriptBlock -ArgumentList $KeyName -Verbose
}
}
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.