# Escaping if statement

**URL:** <https://forums.powershell.org/t/escaping-if-statement/20864>\
**Category:** PowerShell Help\
**Created:** [November 8, 2022, 4:18pm UTC](https://forums.powershell.org/t/escaping-if-statement/20864 "2022-11-08T16:18:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![J1234](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@J1234](https://forums.powershell.org/u/J1234)\
**Post date:** [November 8, 2022, 4:18pm UTC](https://forums.powershell.org/t/escaping-if-statement/20864/1 "2022-11-08T16:18:20Z")

</div>

Hi - I am by no means a powershell regular user, but recently there has become a requirement for me to implement some PS into my work.  
I am trying to safely escape an if statement, as per noted security requirements:  
The original statement:

```auto
if (Get-WmiObject Win32_UserAccount -Filter "Name=\'guest12345678\'") { exit 1 }

```

The statement once escaped;

```auto
if` `$(Get-WmiObject` Win32_UserAccount` -Filter` \\`\"Name=`'guest12345678`'\\`\")` {` exit` 0` }

```

The error I am getting:

```auto
The module 'Get-WmiObject Win32_UserAccount -Filter could not be loaded. For more information, run 'Import-Module Get-WmiObject Win32_UserAccount -Filter '.
Debug: At line:1 char:6
Debug: + if` (Get-WmiObject` Win32_UserAccount` -Filter` \`"Name=`'guest123456 ...
Debug: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Debug: + CategoryInfo: ObjectNotFound: (Get-WmiObject W...uest12345678'\":String) [], CommandNotFoundException
Debug: + FullyQualifiedErrorId : CouldNotAutoLoadModule

```

As mentioned im by no means a regular user, so I must clearly be missing something 🙂 Has anyone any ideas? I appreciate any help with this!

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 8, 2022, 6:00pm UTC](https://forums.powershell.org/t/escaping-if-statement/20864/2 "2022-11-08T18:00:37Z")

</div>

Why are you trying to escape anything? The error indicates it thinks your command is some module. I think there might be some code missing?

---

<div class="post-metadata">

**Author:** ![J1234](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@J1234](https://forums.powershell.org/u/J1234)\
**Post date:** [November 8, 2022, 6:05pm UTC](https://forums.powershell.org/t/escaping-if-statement/20864/3 "2022-11-08T18:05:53Z")

</div>

This is only an example command. The logic implemented is accepting an array of commands and arguments which are then joined to form a string (the input command), and the escaping is to reduce the vulnerability of injection

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [November 8, 2022, 6:46pm UTC](https://forums.powershell.org/t/escaping-if-statement/20864/4 "2022-11-08T18:46:14Z")

</div>

> [@J1234](#):
>
> and the escaping is to reduce the vulnerability of injection

For my curiosity, as I always like to learn about PowerShell, can you explain this in more detail please?

Thanks.

---

<div class="post-metadata">

**Author:** ![Matt](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Matt](https://forums.powershell.org/u/Matt)\
**Post date:** [November 9, 2022, 5:46am UTC](https://forums.powershell.org/t/escaping-if-statement/20864/5 "2022-11-09T05:46:39Z")

</div>

Wonder if this might be helpful

[Protecting Against Malicious Code Injection - PowerShell Team (microsoft.com)](https://devblogs.microsoft.com/powershell/protecting-against-malicious-code-injection/)

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [November 9, 2022, 5:13pm UTC](https://forums.powershell.org/t/escaping-if-statement/20864/6 "2022-11-09T17:13:51Z")

</div>

> [@Matt](#):
>
> Wonder if this might be helpful

Totally makes sense, thank you.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/escaping-if-statement/20864/7 "2024-05-16T20:16:24Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


