Author Posts

August 20, 2013 at 7:30 am

i'm trying to store the output the powershell script into text file...we can do this way when executing the script

powershell test.ps1 > testlog.txt

but i would like to do it within the powershell and my PS script has too many commands and it does lot of there a way to achieve this ?
Please let me know

Thanks in advace

August 20, 2013 at 8:05 am

There are a few different ways to output the data from your script, but the best way to see how to do so would require you to post your code so we can see what data you're pulling.

for general purposes, you can use the add-content command to create the file and add your data to it.

$strCMP=GET-ADComputer ComputerNameHere –Properties cn,MemberOf
$strGRP=($strCMP.MemberOf | Sort Name | foreach {($_.Split(','))[0].Substring(3)}) -Join ","
$strCMP=($ | foreach {($_.Split('='))[0]}) -Join ","
Add-Content c:\outputfiles\CompandGroups.txt -value $strdone

This script pulls the computername and the groups it is assigned to from Active Directory and then writes that information to a text file.

I could also use this code to save to a filename with a .csv extension to make it easier to view the data in MS Excel or another spreadsheet.

August 20, 2013 at 8:54 am

Thanks Notarat for your valuable input.....
Most of my script output is from write-output...please find the attached PS script and below is how the output looks like...

Proceeding with the restore
User logged in successfully
Destination Database Informatica_Test9 exists
Successfully dropped the destination DB Informatica_Test9


Check the job status using: .\qlist job -co s -j JobID

August 20, 2013 at 8:55 am

This is one of the first questions I asked when I started working with PowerShell a few months ago. There's not really a perfect solution at the moment, but I have a couple of modules up on the Script Center that might help. (One is written purely in PowerShell, but has less functionality. The second is written in C# and uses Reflection to intercept PowerShell Host output; it is a more complete solution, but may stop working and require updates if Microsoft makes changes to PowerShell's implementation at some point.)

August 20, 2013 at 9:01 am

Please note that we can't accept PS1 file attachments. Please rename your script file to TXT, or include it in a ZIP file, to attach it. You can also paste your script at, and then provide a link to it here.

August 20, 2013 at 10:23 am

Thanks Don. Please find the attached ps script...and Thanks a lot Dave for the links....could you also please take a look at my file and suggest me how to get the output in a single file with in the scrip ?

Thanks in advance

August 20, 2013 at 2:45 pm

I'm not sure what you mean by "get the output in a single file within the script". Do you mean all of the output should wind up in a single text file? Or do you mean all of the code should be in a single script file, without having to import modules or dot-source anything? (Or something else entirely?) Please clarify. 🙂

August 21, 2013 at 5:10 am

Thanks Dave, i meant all of the output should wind up in a single text file....Please let me know if you have any questions....

August 21, 2013 at 7:02 am

Here is code I use. The if($Host.Name...) will start the transcripting only if not running in the ISE, since ISE does not support transcript files.

$now=Get-Date -format "yyyyMMdd_HHmmss"

if ($Host.Name -ne "Windows PowerShell ISE Host")
$TranscriptFile = $ROOT + "LOGS\" + $MyInvocation.MyCommand.Name.Replace(".ps1", "") + "_$now.log"
Start-Transcript -Path $TranscriptFile
if ($Host.Name -ne "Windows PowerShell ISE Host")

August 21, 2013 at 7:18 am

Both of the modules I posted can do that, though the second one is easier to use (requires fewer changes to your existing script):

# At beginning of script (either in the Begin block, if you're using begin/process/end, or just after the param() block)

# This syntax assumes you've placed the PSLogging module in your modules directory.  If not, you can specify an absolute
# path to the folder or PSLogging.psd1 file instead of just 'PSLogging' in the Import-Module command.

Import-Module PSLogging
$logFile = Add-LogFile -Path C:\Path\To\LogFile.log

# ... Your existing script code

# At the end of the script

$logFile | Disable-LogFile

# Output should have been captured to the 'C:\Path\To\LogFile.log' file.

August 21, 2013 at 7:22 am

Start-Transcript is another option. The main disadvantages of that command versus the PSLogging module are that Start-Transcript doesn't work from the ISE, and you can only have one transcript going for the entire session (which may become an issue if you're calling multiple scripts or functions that all try to do their own logging via Start-Transcript). The PSLogging module supports multiple simultaneous subscribers that mostly don't interfere with one another (except when calling Suspend-Logging and Resume-Logging, which is a global setting).

August 21, 2013 at 10:01 am

Thansk a lot David and Doug for your quick response.. you guys are really awesome and helpful....