Protect Your PowerShell Scripts with Version Control

Doctor Scripto

Summary: Guest blogger and Microsoft PFE Stefan Stranger talks about using version control to protect your Windows PowerShell scripts.

Microsoft Scripting Guy, Ed Wilson, is here. Today I am pleased to present a guest blog post written by Senior Premier Field Engineer Stefan Stranger. The Scripting Wife and I were privileged to hear Stefan speak at the first-ever Dutch Windows PowerShell user group meeting. Following Stefan’s presentation, I asked him if he would write up his presentation as a guest blog post. Today is the result of that request.

Take it away, Stefan …

Protect your PowerShell scripts with version control

During the first Dutch PowerShell User Group meeting, I gave a presentation about protecting your Windows PowerShell scripts with version control, and this is a blog post about how you can get started implementing version control.

Software developers would not think about working on a project without some form of version control. So why is this so unusual for the IT professional who is responsible for maintaining Windows PowerShell scripts containing hundreds of lines of scripts? Version control or source control are terms used for the practice of tracking changes in the source code. There are several tools that can help you implement version control such as GIT, Mercurial, and Team Foundation Server. This blog post explains how you can protect your Windows PowerShell scripts by using Team Foundation Server (TFS) for version control. After implementing version control you will never have to worry that you did not copy your script after you made some changes in your script and it no longer works.

What is version control?

With version control, you can:

  • Record changes
  • Review older versions
  • Undo changes

Other names used for version control are:

  • Revision control
  • Source control

Version control software

Microsoft Visual Studio Team Foundation Server 2012 (TFS) is the collaboration platform at the core of Microsoft’s application lifecycle management (ALM) solution. TFS supports agile development practices, multiple IDEs and platforms locally or in the cloud, and it gives you the tools you need to effectively manage software development projects throughout the IT lifecycle.

Team foundation components

Team Foundation Server consists of the following components:

  • Client tier (like Visual Studio, Office, command line, others)
  • Application tier (Team Foundation Web Services API)
  • Data tier (SQL Server)

TFS source control steps

When you want to start implementing version control, here are some basic commands you need to know:

  • Add ‒ Adds files and folders from a location in the local file system to a version control server for Team Foundation.
  • Check out ‒ Makes the local file writable and changes its pending Change status to “edit” in the workspace.
  • Check in ‒ Commits a pending change to a Team Foundation Server repository.

Note   There are many more commands, but this will get you started with some basic version control activities.

What do you need to get started with version control?

Microsoft offers Team Foundation Service, which is a cloud-based Team Foundation Service. At the moment, it is free for up to five users, and for a limited time, all use is free! You can also install TFS 2012 locally. For more information, see Team Foundation Server. There is also an Express version available to get started.

In this blog post, we are going to set up the following version control environment on a computer running Windows 8:

Step 1: Setting up Team Foundation Service

Start with the sign up.

Image of Team Foundations Service

After the initial setup, we need to create a Project. A Project is a repository for source code and work items.

Image of Creating New Team Project


Image of Create New Team Project dialog box

Step 2: Installing and configuring Team Explorer 2012

You first need to download Team Explorer for Microsoft Visual Studio 2012 and run the installation.

Image of Team Explorer installation

Open Team Explorer, and click Connect to Team Foundation Server.

Image of Team Explorer Home

Enter the URL of your cloud-based Team Foundation Server.

Image of Add Team Foundation Server dialog box

Now it’s time to create a workspace mapping. Go to Source Control Explorer in Team Explorer and map your local drive. You can choose or create every folder you want.

Image of Map dialog box

Step 3: Microsoft Visual Studio Team Foundation Server 2012 Update 1 Power Tools

We are going to use the Windows PowerShell cmdlets from the Microsoft Visual Studio Team Foundation Server 2012 Update 1 Power Tools to communicate from within Windows PowerShell with Team Foundation Server.


  • The Windows PowerShell cmdlets need the .NET Framework 3.5 Windows Feature installed.
  • Windows PowerShell Execution Policy needs to be configured. Restricted Execution Policy does not work.

After downloading the Power Tools, you need to install the Windows PowerShell cmdlets.

Image of Custom Setup

Version control steps in Windows PowerShell

After installing the prerequisites, you can get started with protecting your Windows PowerShell script with version control.

Open the Windows PowerShell ISE, and then load the Team Foundation cmdlets from the Visual Studio Team Foundation Server 2012 Power Tools.

Get-PSSnapin –Registered

Add-PSSnapin Microsoft.TeamFoundation.PowerShell

 Image of command output

Check the commands that are available in the Microsoft.TeamFoundation.PowerShell snap in.

Get-Command –Module Microsoft.TeamFoundation.PowerShell

Image of command output

In this blog post, we are going to use the following cmdlets for controlling versions of your Windows PowerShell scripts. 

  • Get-TfssServer
  • Add-TfsPendingChange
  • New-TfsChangeSet
  • Add-TfsPendingChange
  • Get-TfsPendingChange
  • New-TfsItemHistory
  • Remove-TfsPendingChange


#Check if Microsoft.TeamFoundation.PowerShell is installed

Get-PSSnapin -Registered


#Import Microsoft.TeamFoundation.PowerShell Snapin

Add-PSSnapin Microsoft.TeamFoundation.PowerShell -Verbose


#Connect to TFS Server

Get-TfsServer -Name “https://[name]”


#Create HelloWorld.ps1 file

New-Item C:\DEV\PowerShell\HelloWorld.ps1 -ItemType File -Force


#Add File

#Adds files and folders from a location in the local file system to a version control server for Team Foundation.

Add-TfsPendingChange -Add -Item C:\DEV\PowerShell\HelloWorld.ps1 -Verbose


#Check in new file

New-TfsChangeset -Item C:\DEV\PowerShell\HelloWorld.ps1 -Comment “Initial Check in” -Verbose


#Check out for editing

#Makes the local file writable and changes its pending Change status to “edit” in the workspace. Edit is an alias for the Checkout command.

Add-TfsPendingChange -Edit -Item C:\DEV\PowerShell\HelloWorld.ps1 -Verbose


#Get Pending change

#Displays information about pending changes to items in one or more workspaces.

Get-TfsPendingChange -Item C:\DEV\PowerShell\HelloWorld.ps1 -Verbose


#Make changes to C:\DEV\PowerShell\HelloWorld.ps1

Set-Content -Path C:\DEV\PowerShell\HelloWorld.ps1 -Value “Hello, World”


#Check in after making changes

New-TfsChangeset -Item C:\DEV\PowerShell\HelloWorld.ps1 -Comment “Added some extra script commands” -Verbose


#Get History

Get-TfsItemHistory -HistoryItem C:\DEV\PowerShell\HelloWorld.ps1 -Server (Get-TfsServer -name “https://[name]”)


#Remove Pending Change

#Stores a set of pending changes, together with pending check-in notes, a comment, and a list of associated work items, on the version control server for Team Foundation without actually checking them in.

Remove-TfsPendingChange -Item C:\DEV\PowerShell\HelloWorld.ps1 -Workspace (Get-TfsWorkspace -Path C:\DEV\PowerShell)

Did you know that you can also load the TFS Client Assemblies to achieve the same as the Team Foundation cmdlets from the Visual Studio Team Foundation Server 2012 Power Tools? 

# Load TFS Client Assembly

[Reflection.Assembly]::Load(‘Microsoft.TeamFoundation.VersionControl.Client, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’)


#Connect to TFS Server



#Create HelloWorld2.ps1 file

New-Item C:\DEV\PowerShell\HelloWorld2.ps1 -ItemType File -Force


#Add File

#Adds files and folders from a location in the local file system to a version control server for Team Foundation.

$WorkstationType = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]

$WorkspaceInfo = $WorkstationType::Current.GetLocalWorkspaceInfo(“C:\DEV\PowerShell”)


# Get Collection

$Collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($WorkspaceInfo.ServerUri)



# Get Workspace. Handler for Workspace changes

$Global:Workspace = $WorkspaceInfo.GetWorkspace($Collection)



#Check in new file

$pendingChanges = $workspace.GetPendingChanges() | Where-Object {$_.LocalItem -eq “C:\DEV\PowerShell\HelloWorld2.ps1”}

$Comment = “Initial version of the HelloWorld2.ps1 file”



#Check out for editing



#Get Pending change

$pendingChanges = $workspace.GetPendingChanges() | Where-Object {$_.LocalItem -eq “C:\DEV\PowerShell\HelloWorld2.ps1”}



#Make changes to C:\DEV\PowerShell\HelloWorld2.ps1

Set-Content -Path C:\DEV\PowerShell\HelloWorld2.ps1 -Value “Hello, World”


#Get Tfs Item property information

$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($WorkspaceInfo.ServerUri)

$vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]) #Version Control Server

$vcs.GetExtendedItems(“C:\DEV\PowerShell\HelloWorld2.ps1”, [Microsoft.TeamFoundation.VersionControl.Client.DeletedState]::NonDeleted, [Microsoft.TeamFoundation.VersionControl.Client.ItemType]::Any)


 #Get History

$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($WorkspaceInfo.ServerUri)

$vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]) #Version Control Server

$vcs.QueryHistory(“C:\DEV\PowerShell\HelloWorld2.ps1”, [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest, 0, ‘None’, $null, $null, $null, 1, $false, $false)


Image of command output 

Have fun using Windows PowerShell and TFS to protect your Windows PowerShell scripts with version control.


Thank you, Stefan. Join me tomorrow when I will present an excerpt from my much anticipated Windows PowerShell 3.0 Step by Step book.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon