title: What is the right way to execute c# code in a powershell script?
body: <p>I'm trying to play/pause media and I found some c# code but when I try to implement it into my powershell script, nothing happens... This is what I tried:</p>
  
  <pre><code>Add-Type @'
  using System;
  using System.Windows;
  using System.Runtime.InteropServices;
  
  namespace UniversalSandbox
  {
      public partial class Form1
      {
          public const int KEYEVENTF_EXTENTEDKEY = 1;
          public const int KEYEVENTF_KEYUP = 0;
          public const int VK_MEDIA_NEXT_TRACK = 0xB0;
          public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
          public const int VK_MEDIA_PREV_TRACK = 0xB1;
  
          [DllImport("user32.dll")]
          public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
  
  
  
  
  
          private void button1_Click(object sender, EventArgs e)
          {
              keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
          }
  
          private void button2_Click(object sender, EventArgs e)
          {
              keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
          }
  
          private void button3_Click(object sender, EventArgs e)
          {
              keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
          }
      }
  }
  '@
  
  [UniversalSandbox.Form1]::button1_Click;
  </code></pre>
  
  <p>I call the play/pause event using [UniversalSandbox.Form1]::button1_Click; but nothing happens... Is my syntax wrong?</p>
  
  <p>(I found the c# code <a href="https://ourcodeworld.com/articles/read/128/how-to-play-pause-music-or-go-to-next-and-previous-track-from-windows-using-c-valid-for-all-windows-music-players" rel="nofollow noreferrer">here</a>)</p>