Uploading a Video to Youtube via PowerShell

Hey guys! I know it’s been a long time since I have posted anything useful (that is, assuming anything I post is useful! haha). While sitting at Starbucks today, I suddenly came up with an idea for a new post though, and after doing a quick bit of research, figured I would write about it! The purpose of today’s post is to show an example of using PowerShell to upload a video to Youtube! This could be useful in a variety of circumstances, but I’ll leave finding the use cases up to each of you individually 🙂 A couple of examples I can think of would be:

  • Batch uploading a folder of videos
  • Automating the upload of videos that may be somehow generated into a folder automatically
  • …. ???

In any event, in my search to find a solution, knowing that PowerShell is based on the Microsoft .NET Framework, I searched for “C# upload youtube”. One of the first results that came up was a blog called “Trails in the Sand,” and more specifically, a post entitled “Programmatically Uploading Videos to Youtube.” This blog article goes into detail about uploading videos to Youtube using C#/.NET, but at the bottom of the post, the author was kind enough to include the source code for a library he had written! As a PowerShell scripter, you’ve probably learned to be very thankful for other people who write and share .NET libraries to do “cool stuff!”

After downloading the “Trails in the Sand Youtube Library,” I opened it up in Visual C# 2008 Express Edition (free, fully-functional version of Visual Studio), and converted the project to Visual Studio 2008 format, using the wizard that pops up. The conversion was successful, so I proceeded to create a developer key for Youtube. You can generate your own developer key at the following URL:


After pasting my developer key into the project under the “devCode” string constant, I compiled the library by hitting “F6.” This created my library under the “binrelease” folder of the project. Next, I opened Quest’s PowerGUI (a great, free PowerShell editor), and started a new script. First things first, using the System.Reflection.Assembly .NET class, I loaded the newly created .NET Youtube library into PowerShell:
[cc lang=”powershell”][Reflection.Assembly]::LoadFile(“c:YouTube.dll”)[/cc]
Next, I instantiated the Trails.YouTube object:
[cc lang=”powershell”]$Yt = New-Object Trails.YouTube[/cc]
Once I had the YouTube object, I defined a string variable to use as an “out” parameter, to capture any resulting error (if you don’t pre-define it, you’ll get an error):
[cc lang=”powershell”]$UploadError = “”[/cc]
Next, we call the Authorize() method, passing our username and password:
[cc lang=”powershell”]$Yt.Authorize(“username”, “password”)[/cc]
And then finally, we upload the video using the Upload() method! Parameters are as follows:

  • Title
  • Description
  • Category (library has a spelling error: “catagory”)
  • Video keywords
  • Path to local video file
  • Error variable (pre-defined variable, used as an “out” parameter)

[cc lang=”powershell”]$result = $Yt.Upload(“Test upload”, “test upload”, [Trails.YouTube+Catagory]::Howto, “keyword1”, “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”, [ref] $UploadError)[/cc]
Optionally, you can print out any resulting error message:
The entire script put together looks like this:
[cc lang=”powershell”]$LibYt = [System.Reflection.Assembly]::LoadFile(“c:\Users\trevor.sullivan\Documents\TrailsintheSand.com-YouTubeLibrary\YouTube\bin\Release\YouTube.dll”)
$Yt = New-Object -TypeName Trails.YouTube
$UploadError = “”
$Yt.Authorize($(Read-Host -Prompt “Please enter username”), $(Read-Host -Prompt “Please enter password”))
$result = $Yt.Upload(“Test upload”, “test upload”, [Trails.YouTube+Catagory]::Howto, “keyword1”, “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”, [ref] $UploadError)

If we wanted to “variable-ize” (yes, I made that up just now) this script a bit more, we could do something like this:

[cc lang=”powershell”]### Define YouTube library path
$YtDll = “c:\Users\trevor.sullivan\Documents\TrailsintheSand.com-YouTubeLibrary\YouTube\bin\Release\YouTube.dll”
### Get YouTube username and password
$YtUser = $(Read-Host -Prompt “Please enter username”)
$YtPass = $(Read-Host -Prompt “Please enter password”)
######## Set up video variables ########
$vTitle = “Video Title”
$vDesc = “Video Description”
$vCat = [Trails.YouTube+Catagory]::Howto
$vKeyword = “keyword1”
$vPath = “C:Userstrevor.sullivanVideosSMS_BootImagePackage.wmv”
$vErr = “”
### Load YouTube library, login, upload video, and echo error
$LibYt = [System.Reflection.Assembly]::LoadFile($YtDll)
$Yt = New-Object Trails.YouTube
$Yt.Authorize($YtUser, $YtPass)
$result = $Yt.Upload($vTitle, $vDesc, $vCat, $vKeyword, $vPath, [ref] $vErr)

Hope this post helps!