I've written a shell script that will exclude a folder during Microsoft Defender Antivirus scanning. I don't have a lot of experience with shell scripting, and this is what I come up with:
Set-MpPreference -ExclusionPath D:\vpostest
echo "Exclusion Success"
Read-Host -Prompt "Press Enter to exit"
The first line of the code is where I mention the exclusion, and the following is just some kind of prompt to notify the user that the exclusion is a success (it is still a work in progress).
Once I run the script, I receive an error:
Set-MpPreference : Operation failed with the following error: 0x%1!x!
At C:\Users\zainur.ariffin\Desktop\Powershell test\autoExclusion.ps1:1 char:1
+ Set-MpPreference -ExclusionPath D:\vpostest
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_MpPreference:root\Microsoft\...FT_MpPreference) [Set-MpPreference], CimException
+ FullyQualifiedErrorId : HRESULT 0xc0000142,Set-MpPreference
If I'm not mistaken, I've read that the following error indicates that Microsoft Defender Antivirus was not enabled. I have already enabled this feature and restarted my computer, and even check with 'Get-MpComputerStatus'. However, the same error keeps appearing. Why is this happening?
You found the solution:
The
Set-MpPreference
cmdlet from theConfigDefender
module must be run with elevation (as administrator), as all cmdlets from this module that modify settings must.It is unfortunate that the error message reported by these cmdlets is so obscure (as of v1.0 of the module); if the need to elevate were stated clearly, the solution would be obvious.
Ways to ensure that your script runs with elevation:
Ad hoc, assuming your script-file path is stored in
$scriptFile
:-NoExit
keeps the elevated session open after your script exits, so you can examine its output.-File
CLI parameter is preferable for invoking script files, and the embedded double-quoting (`"...`"
) ensures that paths with spaces are handled correctly.C:\Windows\System32
(if you usedpwsh
, the PowerShell (Core) CLI instead, the caller's working directory would be preserved); if that is a problem, either modify your script to change the location or use a-Command
(-c
) CLI call that changes the location before invoking your script - see this answer.Using self-elevation, i.e. modifying your script so that it re-invokes itself with elevation on demand, by placing the following at the start of your script:
Note: This assumes that your script doesn't need to support parameters; if it does, a robust solution requires a lot more effort: see this answer.
The automatic
$PSCommandPath
variable contains the full path of the running script.(Get-Process -Id $PID).Path
is used in lieu of hard-codingpowershell
to ensure that you're using the same PowerShell executable that was used to launch the script; that is, if you launched from Windows PowerShell, it'll usepowershell.exe
, if you launched from PowerShell (Core) 7+, it'll usepwsh.exe
.Creating a shortcut file (
*.lnk
) interactively:Since
*.ps1
files are not directly executable from outside PowerShell, using the path of such a file alone as the shortcut target won't work: it'll be treated as a document to open for editing instead.Run as administrator
checkbox is disabled in theAdvanced...
(Advanced Properties
) sub-dialog.Therefore, you must call the script explicitly via PowerShell's CLI; specify the following target in combination with checking
Run as administrator
:Note that programmatic creation of a shortcut file, via the
WScript.Shell
COM object is unfortunately not an option, because it doesn't expose a property to request running as administrator.