Disclaimer: this is not secure. If you want secure, you need to encode the password as a secure string in a supporting file. This is for when you have to have the password in the script, but you want to hide it in plain sight.
The trick: obfuscation.
First off, go to http://home2.paulschou.net/tools/xlate/, put your password in the TEXT field, and get the HEX values. If your password is “Password”, the HEX will be 50 61 73 73 77 6f 72 64
Next, generate a very long string of random characters to use. I use my Generate-Password function to make a 48 character password of every printable character (with the Consolas font, anyway) in the standard ASCII set:
$charSet = [string]::Join("", (((33..126)+(161..255)) | %{[char]$_}))
Generate-Password -Length 48 -Chars $charSet
This leaves me with something like Ý×ôg’ÐÚ®ÉFS)ÉAÓ<ɨc¦TÊiÝØ¨ü*T&½Â{íÏÄüïN¾m!bhm0¦
Next, we need to combine these together. In your random string, find a place where there are no lower-case hexadecimal characters for the length of your password +1. In this case, it’s the string ®ÉFS)ÉA. It can be anywhere in your string; it just has to be clean. After the first character of the selected substring, enter your first Hex character pair (50 in this case.) Skip a character, enter the next one, and so on. When you are done, you should have a combined string like Ý×ôg’ÐÚ50®61É73F73S77)6fÉ72A64Ó<ɨc¦TÊiÝØ¨ü*T&½Â{íÏÄüïN¾m!bhm0¦
Note: I picked 48 characters because my 8 char password produces 16 hex chars, making a nice 64-digit block of seemingly-meaningless text. The whole point is to add as many layers of obfuscation as possible; even someone counting characters could be led nicely off-track for a while.
Now that you have this nice ridiculous string, assign it to a variable with a misdirecting name:
$Temp2 = &quot;Ý×ôg'ÐÚ50®61É73F73S77)6fÉ72A64Ó&lt;ɨc¦TÊiÝØ¨ü*T&amp;½Â{íÏÄüïN¾m!bhm0¦&quot;
It helps to also do the same for the username called $temp3, and maybe one or two that won’t be used.
Now that you have a nicely encoded, obfuscated password, you just need the script to be able to decode it. Run:
($Temp2 -creplace &quot;[^0-9a-f]&quot;,&quot;#&quot;).Split(&quot;#&quot;)
Note the use of -creplace instead of -replace; case-sensitive regex really matters here. You should get a whole bunch of blank lines, with perhaps a few single Hex characters, and somewhere in the middle, your Hex string doubles. What we did was find all the characters that did not match 0..9 or a..f and replace them with a character, then split the string into an array on that character. I picked the Hash because it has meaning in PoSH, but we are not using that meaning in this context, thus providing further obfuscation.
Now count your way to the first pair of your set (remember to start at 0) and the last pair. In my example case, it’s 8 and 15. Now, add a few lines to the end of your previous string:
($Temp2 -creplace &quot;[^0-9a-f]&quot;,&quot;#&quot;).Split(&quot;#&quot;)[8..15]
If you did it right, you just got your original Hex string, only as an array instead of a string separated by spaces. We’re just narrowing to only those members of the array.
We’re almost there. To get the password back, we need to convert each of these hex pairs into an ASCII character, and then change this array of ASCII characters into a single string.
[string]::join(&quot;&quot;, (($Temp2 -creplace &quot;[^0-9a-f]&quot;,&quot;#&quot;).Split(&quot;#&quot;)[8..15] | %{[convert]::ToInt32($_, 16)} | %{[char]$_}))
The [convert] portion of the pipeline converts the Hex (aka Base16) characters to Int32. The [char] portion converts the integers to their ASCII equivalents. And the whole pipeline is contained in parentheses within the [string]::Join command, which joins the array as a string. The output is the password we originally encoded.
Can this be decoded? Yes. Easily? No. Few have the knowledge, nor the patience to track backwards through this morass to figure out what the password is. When paired with an account with permissions only to the very narrowest possible resources needed to run the script, this will work as a “secure enough, good enough” sort of free solution.