SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As part of a new SharePoint 2010 project I am trying to automate the build of the SharePoint site on our staging system using NAnt, something we use a lot with our C# builds.

I have created a PowerShell script that I have tested successfully:-

#Load SharePoint cmdlets
& "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG POWERSHELL\Registration\sharepoint.ps1"

# Create the site based on blank site template (STS#1)
$template = Get-SPWebTemplate "STS#1"
$date = Get-Date -format "yyyy-MM-dd HHmm"
$siteURL = "http://MY-PC/SITE " + $date
$siteName = "SITE " + $date
New-SPWeb -Url $siteURL -Template $template -Name $siteName -AddToTopNav

I then created a NAnt file with the following exec call:-

    <exec failonerror="true" program="${powershell.exe}">   
    <arg value="-noprofile" />
    <arg value="-nologo" />
    <arg value="-noninteractive" />
    <arg value="-command" />    
    <arg value=" &quot; &amp; &apos;${createsite.ps1}&apos; &quot; " />

Where ${powershell.exe} -> C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe and ${createsite.ps1} is the full path of my powershell script.

But when I run this I get the following error:-

 [exec] Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version
 [exec] 2.
 [exec] At C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONF
 [exec] IG\POWERSHELL\Registration\sharepoint.ps1:3 char:13
 [exec] + Add-PsSnapin <<<<  Microsoft.SharePoint.PowerShell
 [exec]     + CategoryInfo          : InvalidArgument: (Microsoft.SharePoint.PowerShel
 [exec]    l:String) [Add-PSSnapin], PSArgumentException
 [exec]     + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
 [exec]    dPSSnapinCommand

I thought this was due to my not referencing the SharePoint PowerShell snap in correctly, so changed

& "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\sharepoint.ps1"

to this

Add-PSSnapin Microsoft.SharePoint.PowerShell

with no effect.

Does anybody have any experience of running PowerShell with NAnt and have any suggestions?



As per Micks reply, the process was indeed calling the 32 bit version of PwerShell. Turns out Microsoft redirects you from the 64 bit folder when running a 32 bit process. For now, I've created an NTFS Junction that points to the 64 bit directory for PowerShell and pointed to that in my NAnt script.

share|improve this question
up vote 4 down vote accepted

I think the script is actually calling the 32bit version of Powershell as opposed to the 64bit version. You may have to directly code the Powershell path into the nAnt scipt.

share|improve this answer
hmmm...yes I can see what you mean. However, I'm definately pointing to the 64 bit version (C:\Windows\System32\WindowsPowerShell\v1.0) rather than the 32 bit (C:\Windows\SysWOW64\WindowsPowerShell\v1.0) and it's strange that the process works fine if I run the command from a DOS window, but not through NAnt. I shall do some more digging around about this though. Thanks. – grimorde Mar 10 '11 at 14:54

Thanks for leading me down the right track.

SysNative seems to work for me, e.g. <property name ="powershell.exe" value="C:\Windows\Sysnative\WindowsPowerShell\v1.0\PowerShell.exe" />

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.