PowerShell Output to a File and some other tidbits
Output to a File

PowerShell Output to a File and some other tidbits

Posted by on Monday, June 3rd, 2013  

 

Whenever you are getting data using PowerShell (in my case, usually things like a list of URLs out of SharePoint), it is a good idea to write them out to a file for reference. It is especially a good idea if you are planning on changing something in case you need to reverse your action.

To write out to a file for PowerShell there are a few different ways to do it (and maybe even more than what I have listed here). Watch out for truncated values as it writes to file exactly what would be seen on the screen. Use the -Autosize and/or -Wrap arguments with a Format- command earlier in the pipeline to remedy this. (http://technet.microsoft.com/en-us/library/dd347677.aspx)

The PowerShell “Out-File” cmdlet which sends piped output directly to a file instead of the screen.

[script] | Out-File C:\filename.txt

The PowerShell “Tee-Object” cmdlet (alias: tee) which sends piped output to both the console and a file at the same time.

[script] | tee C:\filename.txt

You can even use the old DOS way which still works using two “greater than” signs (>>) followed by the full path to the file. (No pipe needed.)

[script] >> C:\filename.txt

Here are some examples that I had to use recently (all single lines).

These scripts run against all sites in all web applications. So depending on the size of the farm, it could take a little time to execute. There are only a few differences in the scripts which I have highlighted for reference. The % (percent sign) is an alias for the ForEach-Object cmdlet (could also be written: foreach). I also use an inline “IF” statement combined with a “SilentlyContinue” ErrorAction to filter the output.

 

Returns a list of all Site Collection Urls that DO NOT have the specified feature enabled (in this case “ViewFormPagesLockdown”):

Get-SPWebApplication | %{$_.Sites} | %{if((Get-SPFeature 
<replacewithfeaturename> -Site $_.Url -ErrorAction SilentlyContinue) -
eq $null ) {$_.Url} } | tee C:\FeatureNotEnabled.txt

 

Returns a list of all Site Collection Urls that DO have the specified feature enabled (in this case “ViewFormPagesLockdown”):

Get-SPWebApplication | %{$_.Sites} | %{if((Get-SPFeature 
<replacewithfeaturename> -Site $_.Url -ErrorAction SilentlyContinue) -
ne $null ) {$_.Url} } | tee C:\FeatureAlreadyEnabled.txt

 

Enables the specified feature for the Site Collections that do not have it enabled (in this case “ViewFormPagesLockdown”):

Get-SPWebApplication | %{$_.Sites} | %{if((Get-SPFeature 
<replacewithfeaturename> -Site $_.Url -ErrorAction SilentlyContinue) -
eq $null ) {Enable-SPFeature -Identity ViewFormPagesLockdown -Url 
$_.Url; $_.Url} } | tee C:\FeatureEnabledSiteList.txt

 

Disables the specified feature for the Site Collections that have it enabled (in this case “ViewFormPagesLockdown”):

Get-SPWebApplication | %{$_.Sites} | %{if((Get-SPFeature 
<replacewithfeaturename> -Site $_.Url -ErrorAction SilentlyContinue) -
ne $null ) {Disable-SPFeature -Identity ViewFormPagesLockdown -Url 
$_.Url -Confirm:$false; $_.Url} } | tee C:\ FeatureDisabledSiteList.txt

BONUS:

You can use the same method to stop shy of getting and interrogating the features and just return the URLs which gives you a master list of all Site Collection URLs or Web Site URLs in the farm.

Get-SPWebApplication | %{$_.Sites} | %{$_.Url} | tee 
C:\AllSiteCollectionUrls.txt
Get-SPWebApplication | %{$_.Sites} | %{$_.AllWebs} | %{$_.Url} | tee 
C:\AllWebSiteUrls.txt

There you have it – various ways to output from PowerShell to a file along with some bonus “IF” filter scripting. As you can tell, my favorite is the Tee-Object (tee) command. Since I discovered it, I use it everywhere (maybe too much), but can you really log too much. I can’t tell you how many times this has helped me…saving the output of a script or at least the values I was going to change with a script prior to running the change. Happy scripting!

 

Disclaimer
The sample scripts are not supported under any Summit 7 Systems standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Summit 7 Systems further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Summit 7 Systems, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Summit 7 Systems has been advised of the possibility of such damages.

Posted by on Monday, June 3rd, 2013  

Subscribe to RSS Feed

Sign Up for Newsletter

Leave a Reply