# \-file not working in an -encodedcommand

**URL:** <https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883>\
**Category:** PowerShell Help\
**Created:** [July 18, 2019, 7:11am UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883 "2019-07-18T07:11:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bazcurtis](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bazcurtis/32/189_2.png) [@bazcurtis](https://forums.powershell.org/u/bazcurtis)\
**Post date:** [July 18, 2019, 7:11am UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883/1 "2019-07-18T07:11:20Z")

</div>

Hi,

I am writing the below to do some testing. When I encode this part of the command it fails. I’m not sure why.

Any help would be much appreciated.

```
powershell.exe -executionpolicy bypass -nologo -encodedCommand LQBmAGkAbABlACAAQwA6AFwAVQBzAGUAcgBzAFwAUAB1AGIAbABpAGMAXABUAHIAaQBnAGcAZQByAF8AQQBXAFMAXwB2ADcALgBwAHMAMQA=
powershell.exe : -file : The term '-file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
At line:1 char:1
```

---

<div class="post-metadata">

**Author:** ![an0nemus](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@an0nemus](https://forums.powershell.org/u/an0nemus)\
**Post date:** [July 18, 2019, 10:45am UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883/2 "2019-07-18T10:45:08Z")

</div>

So, that’s ‘interesting’. I can say I haven’t worked with encoded commands, but this is a work around.

&nbsp;

&nbsp;

```
powershell -command invoke-expression .\test.ps1
hello world
$text="invoke-expression .\test.ps1"
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($text), 'InsertLineBreaks')
aQBuAHYAbwBrAGUALQBlAHgAcAByAGUAcwBzAGkAbwBuACAALgBcAHQAZQBzAHQALgBwAHMAMQA=
C:\TEST>powershell -encodedcommand aQBuAHYAbwBrAGUALQBlAHgAcAByAGUAcwBzAGkAbwBuACAALgBcAHQAZQBzAHQALgBwAHMAMQA=
hello world
```

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [July 18, 2019, 7:25pm UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883/3 "2019-07-18T19:25:01Z")

</div>

FYI…

Encoded commands in an enterprise environment is frowned on an mostly never allowed, because this is what hackers use to make their efforts. Encode string never remain encoded anyway, especially if PowerShell Auditing / logging is enabled. It will decode it and it will show as plain text in the logs / audit reports.

So, doing this as a learning effort is cool and all, doing it to try and protect your code from modification, maybe, doing it to prevent your code from being captured / read / reused by someone else, well, it’s not effective / easily reversed.

It’s more prudent to PS2EXE and the like to achieve the aforementioned, depending on your overall goals / needs.

---

<div class="post-metadata">

**Author:** ![bazcurtis](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bazcurtis/32/189_2.png) [@bazcurtis](https://forums.powershell.org/u/bazcurtis)\
**Post date:** [July 19, 2019, 2:02am UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883/4 "2019-07-19T02:02:46Z")

</div>

> [@](#):
>
> Encoded commands in an enterprise environment is frowned on an mostly never allowed, because this is what hackers use to make their efforts. Encode string never remain encoded anyway, especially if PowerShell Auditing / logging is enabled. It will decode it and it will show as plain text in the logs / audit reports.

I totally agree. The reason for my testing is, our EDR product can search the customers estate and look for PowerShell commands/scripts that are being run. I have written a test workflow where a Word Macro will download a VB script from AWS, that will download a PowerShell script from AWS and run. I actually wanted to put this command in the VB script, but it failed. I then went to test it within PowerShell itself and realised it didn’t work, hence the question.

These files just trigger fake detections allowing me to show the customers what is going on. If the customer has restrictions in place then that is fantastic, if not, I can explain why it is bad.

&nbsp;

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [July 19, 2019, 1:16pm UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883/5 "2019-07-19T13:16:27Z")

</div>

Ah, Roger that!

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [July 21, 2019, 9:51pm UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883/6 "2019-07-21T21:51:40Z")

</div>

You don’t need to use -file in the encoded command, if you do so then it becomes

```
ps \> -file c:\temp\bla.ps1
```

You need to only encode c:\temp\bla.ps1

---

<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:31pm UTC](https://forums.powershell.org/t/file-not-working-in-an-encodedcommand/12883/7 "2024-05-16T20:31:54Z")

</div>

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


