Table of Contents
Using Invoke-Item
Cmdlet
Use the Invoke-Item
cmdlet to run the .reg
file using PowerShell.
1 2 3 |
Invoke-Item ".\file.reg" |
For the above command, Invoke-Item performed its default action, which means it allowed us to execute a .reg
file. We can also use it to run a file, for instance, a script file.
When we ran the Invoke-Item
cmdlet with a .reg
file as the argument, it executed the file and applied the changes contained in the file to the registry.
We can also use the -Confirm
parameter to ensure action. The -Confirm
parameter is a standard parameter in PowerShell cmdlets that displays a prompt asking you to confirm an action before it is executed.
For example, we used this parameter with the Invoke-Item
cmdlet to display a prompt asking the user to confirm that you want to execute the .reg
file before it is run. For instance:
1 2 3 |
Invoke-Item ".\file.reg" -Confirm |
Using PowerShell cmdlets with the -Confirm
parameter can be helpful if we are unsure of the effects of the changes contained in the .reg
file or if we want to ensure that the users are aware of the changes that are being made to the registry.
Using Start-Process
Cmdlet
Use the Start-Process
cmdlet to run the .reg
file using PowerShell.
1 2 3 |
Start-Process "regedit.exe" "/s .\file.reg" -NoNewWindow -Wait |
Here, we used Start-Process cmdlet, which is used to start one or multiple processes on the local machine, such as a batch file or an executable. For example, we used the Start-Process
cmdlet to start regedit.exe
with the /s
flag and a .reg
file as the arguments; it will execute the regedit
command and apply the changes contained in a .reg
file to the registry.
Here, the /s
flag is an argument used with the regedit
command to specify that the command should be executed and then quit rather than opening the user interface. This flag is often used in conjunction with a .reg
file to import the file’s contents into the registry.
The -NoNewWindow
parameter tells Start-Process
to open the new process in the current window rather than opening a new window. On the other hand, the -Wait
parameter informs Start-Process
to wait for a process to exit before continuing to the following command.
Using iex
Operator
Use the iex
(Invoke-Expression) operator to run the .reg
file using PowerShell.
1 2 3 |
iex "& regedit.exe /s .\file.reg" |
This command is similar to the previous one. It also executes the regedit
command and applies the changes contained in the .reg
file to the registry when we use the iex
operator to run the regedit
command with the /s
flag and a .reg
file as arguments.
We have already learned about the/s
flag using the Start-Process
cmdlet. Here, we used the &
operator to execute a command, script, or script block. Then, we used it with the iex
(Invoke-Expression) operator to run a command as a string.
NOTE: The
&
operator is often used to run external programs, such as executables, from within a PowerShell script or console. It can also be used to run scripts and script blocks.
Further reading:
Using .
Operator
Use the .
(Dot) operator to run the .reg
file using PowerShell.
1 2 3 |
. ".\file.reg" |
The .
(dot) operator runs a script or script block. We used it with a .reg
file as the argument to execute the file and update the registry with the specified .reg
file’s content.
Using reg import
Command
Use the reg import
command to run the .reg
file using PowerShell.
1 2 3 |
reg import .\file.reg |
The above code used the reg import
command, a command-line utility allowing us to import a .reg
file into the registry. Here, the .reg
file contains instructions for deleting, adding, or updating keys and values in the registry. We can use this command to execute the reg import
command and apply the modifications contained in the .reg
file to the registry.
The .\
denotes the current directory; you can also use the absolute path as follows:
1 2 3 |
reg import C:\file.reg |
We can use the Get-Command
if you don’t know the absolute path of reg
because this cmdlet retrieves information about commands (functions, scripts, cmdlets, and executables). We used it to display information about the reg
command.
1 2 3 4 5 |
CommandType Name Version Source ----------- ---- ------- ------ Application reg.exe 10.0.19... C:\WINDOWS\system32\reg.exe |
Editing the registry manually is generally not recommended, as incorrect changes can cause serious problems. Instead, if you require changes to the registry, it is usually safer to use a program or script specifically designed for that purpose. This is where we should know the pros and cons of every approach we learned above before using them in a script/program.
Command/Cmdlet/Operator | Pros | Cons |
---|---|---|
Invoke-Item |
It is easy to use and does not need any additional arguments. | It does not provide a way to write options or arguments for the file being run. |
Start-Process |
It provides several options and arguments for customizing the process. | It can be more difficult to use than other options. |
iex (Invoke-Expression) |
It is useful for executing external programs from within a PowerShell script or console. | It requires us to specify the full command as a string. |
. (Dot) |
It is one of the easier approaches and does not require extra (additional) arguments. | It is preferred to run scripts, not executables or other types of files, because it may result in unexpected outcomes. |
reg import |
It is also easy to use and does not need additional arguments. | It requires us to open a command prompt to run the command and does not provide a way to specify options or arguments for the .reg file being imported. |
That’s all about how to run .reg File Using PowerShell.