title: PowerShell script fails first time but works second time
body: <p>I've found a <a href="http://poshcode.org/2603" rel="noreferrer">PowerShell script</a> that can change the desktop wallpaper of my Windows 7 PC of an image file whose path is supplied as a parameter. The end result of what I want is to have this script called by a batch file on startup.</p>
  
  <pre><code>[CmdletBinding()]
  Param(
     [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
     [Alias("FullName")]
     [string]
     $Path
  ,
     [Parameter(Position=1, Mandatory=$false)]
     $Style = "NoChange"
  )
  
  BEGIN {
  try {
     $WP = [Wallpaper.Setter]
  } catch {
     $WP = add-type @"
  using System;
  using System.Runtime.InteropServices;
  using Microsoft.Win32;
  namespace Wallpaper
  {
  public enum Style : int
  {
       Tile, Center, Stretch, NoChange
  }
  
  public class Setter {
    public const int SetDesktopWallpaper = 20;
    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
  
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  
    public static void SetWallpaper ( string path, Wallpaper.Style style ) {
       SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  
       RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
       switch( style )
       {
          case Style.Stretch :
             key.SetValue(@"WallpaperStyle", "2") ; 
             key.SetValue(@"TileWallpaper", "0") ;
             break;
          case Style.Center :
             key.SetValue(@"WallpaperStyle", "1") ; 
             key.SetValue(@"TileWallpaper", "0") ; 
             break;
          case Style.Tile :
             key.SetValue(@"WallpaperStyle", "1") ; 
             key.SetValue(@"TileWallpaper", "1") ;
             break;
          case Style.NoChange :
             break;
       }
       key.Close();
      }
     }
  }
  "@ -Passthru
  }
  }
  PROCESS {
     Write-Verbose "Setting Wallpaper ($Style) to $(Convert-Path $Path)"
     $WP::SetWallpaper( (Convert-Path $Path), $Style )
  }
  </code></pre>
  
  <p>I'm calling this script using the command:</p>
  
  <blockquote>
    <p>C:\scripts\Set-Wallpaper.ps1 C:\Users\myProfile\Pictures\MyWallpaper.jpg</p>
  </blockquote>
  
  <p>I'm totally new to the world of PowerShell scripts, and the issue I'm having is that when I execute the script from within PowerShell it always fails the first time with the following error:</p>
  
  <blockquote>
    <p>C:\scripts\Set-Wallpaper.ps1 : Unable
    to cast object of type
    'System.Object[]' to type
    'System.Type'.</p>
    
    <p>At line:1 char:29 </p>
    
    <ul>
    <li>C:\scripts\Set-Wallpaper.ps1 &lt;&lt;&lt;&lt;  C:\Users\mbaleato\Pictures\MyWallpaper.jpg
    
    <ul>
    <li>CategoryInfo          : NotSpecified: (:) [Set-Wallpaper.ps1],
    InvalidCastException</li>
    <li>FullyQualifiedErrorId : System.InvalidCastException,Set-Wallpaper.ps1</li>
    </ul></li>
    </ul>
  </blockquote>
  
  <p>But when I call the script with the exact same command and parameter the second time it works.</p>
  
  <p>It is this failing the first time that is causing my batch file to fail. </p>
  
  <p>Anyone who is more experienced have some suggestions as to why it fails the first time, but works the second time? Any suggestions as to how I can get it to work  the first time?</p>