I seem to be experiencing a DLL import error only when the following powershell command is executed from the run dialog. The exact same command from an open powershell window executes without an issue.
Error producing RUN dialog command:
Powershell $k=Add-Type -MemberDefinition '[DllImport("user32.dll")]public static extern bool ShowWindow(IntPtr h, int n);' -Name w -PassThru
The following error is printed:
No errors are returned when running the same code in an already open powershell window:
$k=Add-Type -MemberDefinition '[DllImport("user32.dll")]public static extern bool ShowWindow(IntPtr h, int n);' -Name w
-PassThru;
I find this beyond confusing as i am under the impression both methods should have produced the same outcome. Is there any logical reasoning to why this is happening? How can this error be resolved?
The purpose of the code is to minimize the powershell window. I am aware that there are other methods to achieve this.
Welcome to the wonderful world of nesting string literals across different languages.
Put double quotes around the entire command line you want executed by PowerShell and escape nested double quotes with backslashes:
powershell "$k=Add-Type -MemberDefinition '[DllImport(\"user32.dll\")]pub...;"
# ^ ^ ^ ^
Addendum: note that the above commandline will not work when invoked from a PowerShell console. In that case you need additional (PowerShell) escapes for $
and "
characters inside the command string:
powershell "`$k=Add-Type -MemberDefinition '[DllImport(\`"user32.dll\`")]pub...;"
# ^ ^ ^