# Convert short script to 1liner (-command format)

**URL:** <https://forums.powershell.org/t/convert-short-script-to-1liner-command-format/3609>\
**Category:** PowerShell Help\
**Created:** [January 9, 2015, 10:09pm UTC](https://forums.powershell.org/t/convert-short-script-to-1liner-command-format/3609 "2015-01-09T22:09:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter-stork](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@peter-stork](https://forums.powershell.org/u/peter-stork)\
**Post date:** [January 9, 2015, 10:09pm UTC](https://forums.powershell.org/t/convert-short-script-to-1liner-command-format/3609/1 "2015-01-09T22:09:54Z")

</div>

Hello guys,

This will be an easy one for you I suppose.  
I’m having trouble converting one of my short scripts to match the format required by the -command parameter.  
The script is:

```
Add-Type -TypeDefinition '
using System;
using System.Runtime.InteropServices;
 
namespace Utilities {
   public static class Display
   {
      [DllImport("user32.dll", CharSet = CharSet.Auto)]
      private static extern IntPtr SendMessage(
         IntPtr hWnd,
         UInt32 Msg,
         IntPtr wParam,
         IntPtr lParam
      );
 
      public static void PowerOff ()
      {
         SendMessage(
            (IntPtr)0xffff, // HWND_BROADCAST
            0x0112, // WM_SYSCOMMAND
            (IntPtr)0xf170, // SC_MONITORPOWER
            (IntPtr)0x0002 // POWER_OFF
         );
      }
   }
}
'
[Utilities.Display]::PowerOff()
```

Originally from [#PSTip How to switch off display with PowerShell](http://www.powershellmagazine.com/2013/07/18/pstip-how-to-switch-off-display-with-powershell/)  
If you want to test, it just turns your display off.  
Appreciate your help!

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [January 9, 2015, 10:43pm UTC](https://forums.powershell.org/t/convert-short-script-to-1liner-command-format/3609/2 "2015-01-09T22:43:37Z")

</div>

You will need to encode your script as Base64 and supply it to the EncodedCommand parameter of PowerShell.exe.

Note: I have replaced SendMessage with SendNotifyMessage because SendMessage is waiting for the message to be processed which does not work for power off.

Below example will encode the script and use the clip.exe to copy it into the Windows clipboard:

```
$Command = @"
Add-Type -TypeDefinition '
using System;
using System.Runtime.InteropServices;
 
namespace Utilities {
   public static class Display
   {
      [DllImport("user32.dll", CharSet=CharSet.Auto)]
      private static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
 
      public static void PowerOff ()
      {
         SendNotifyMessage(
            (IntPtr)0xffff, // HWND_BROADCAST
            0x0112, // WM_SYSCOMMAND
            (UIntPtr)0xf170, // SC_MONITORPOWER
            (IntPtr)0x0002 // POWER_OFF
         );
      }
   }
}
'
[Utilities.Display]::PowerOff()
"@

$EncodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Command))
'PowerShell.exe -EncodedCommand "{0}"' -f $EncodedCommand | clip
```

---

<div class="post-metadata">

**Author:** ![peter-stork](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@peter-stork](https://forums.powershell.org/u/peter-stork)\
**Post date:** [January 9, 2015, 11:11pm UTC](https://forums.powershell.org/t/convert-short-script-to-1liner-command-format/3609/3 "2015-01-09T23:11:55Z")

</div>

Perfect, thank you sir!

---

<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:46pm UTC](https://forums.powershell.org/t/convert-short-script-to-1liner-command-format/3609/4 "2024-05-16T20:46:32Z")

</div>

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


