3

Hello i am doing a small script will run a small powershell using Add-MpPreference. In my case, i was doing because sometimes i want to include some path's in my windows 10 but i must to do manually. So i was trying to make it automatic. I did this:

ShellExecute("powershell", '-noexit Add-MpPreference -ExclusionPath C:/')

Error return's me:

The term 'Add-MpPreference' is not recognized as the name of a cmdlet, function
, script file, or operable program. Check the spelling of the name, or if a pat
h was included, verify that the path is correct and try again.
At line:1 char:17
+ Add-MpPreference <<<<  -ExclusionPath C:/
    + CategoryInfo          : ObjectNotFound: (Add-MpPreference:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

What mean's this error? Or can be fixed?

user4157124
  • 2,809
  • 13
  • 27
  • 42
Sir Jack
  • 227
  • 3
  • 15
  • does this help? http://stackoverflow.com/questions/34148182/not-recognizing-script-name-as-cmdlet-function-etc-nor-can-positional-peramet – Xenobiologist May 10 '17 at 12:45

1 Answers1

7

I had a similar issue. I discovered that when running Powershell in 32 bit mode on a 64 bit OS it doesn't find the command Add-MpPreference.

You can easily recreate this by opening a Powershell console in 32 bit mode and call this function. It will give the same error.

The issue occurred because I was running the Powershell script from C# code that was running as 32 bit. This caused the powershell.exe that was called to also be the 32 bit version.

I adjusted the code to call the 64 bit version and then the command was found.

In order to do this have it run Powershell from here C:\Windows\SysNative\WindowsPowerShell\v1.0\powershell.exe

For more information about how to set the version of powershell visit https://web.archive.org/web/20180314210451/http://www.madwithpowershell.com/2015/06/64-bit-vs-32-bit-powershell.html

dosxuk
  • 73
  • 1
  • 3
Zvi Bogen
  • 71
  • 1
  • 5
  • I had the same problem, and your solution worked, I just wanted to add that obviously not all Windows 10 versions are affected: I could reproduce the behaviour in build 1607, but not in 1809, so it seems Microsoft has fixed this in newer versions. Can anybody confirm this observation? – Erik Oct 26 '20 at 10:06