6

I am using this script to add users to folder permission. How can I output the result of this script (success or failure) to a text file?

I tried adding 2> "OutputPath" at the end to stream the output to this location but it did not work.

$ProjectName = "ProjetName"
$Folder = "FolderPath" + $ProjectName
Get-Acl $Folder
$ColRights = [System.Security.AccessControl.FileSystemRights]"Modify, ListDirectory"
$InheritanceFlagSetting = @("ContainerInherit","ObjectInherit")
$PropagationFlagSetting = "None"
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]$InheritanceFlagSetting
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]$PropagationFlagSetting
$objType =[System.Security.AccessControl.AccessControlType]::Allow
$objUser = New-Object System.Security.Principal.NTAccount("Username")
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)

#Get the ACL for the folder (so we can add to it)
$objACL = Get-Acl $Folder
$objACL.AddAccessRule($objACE)

#publish the new ACL with additional rule
Set-Acl $Folder $objACL
2
  • 3
    Possible duplicate of Write output to a text file in PowerShell Commented Apr 20, 2019 at 16:33
  • 2
    What output are you expecting? Your invocations of Get-ACL, Set-ACL, and $objACL.AddAccessRule() (which doesn't return a value) are the only expressions not captured in variables, so that's all you should see. Why are you using 2> specifically? Commented Apr 20, 2019 at 16:34

2 Answers 2

10

Using:

| Out-File -FilePath C:\FileName.log

At your point of execution will create a new file or overwrite an existing file with the same name. It is similar to using

> C:\FileName.log

These commands are used when you only have a single point of execution to capture or when you want to overwrite an existing file.

Using:

| Out-File -FilePath C:\FileName.log -Append

At your point of execution will create a new file if it doesn't already exist, or if it does exist, it writes to the end (Appending) of the file. It is similar to using

>> C:\FileName.log

These methods are used when you want to continue using the same file.

The other option is to use:

Start-Transcript -Path C:\FileName.log 

(which can also be used with -Append) at the start of your script. When you start this, it will log everything that follows, including the code in your script. To turn it off you need to use the "Stop-Transcript" command or end the session.

9

You may use Start-Transcript and the beginning of your script and stop transcript at the end. This will capture your script execution in a file you mention.

Start-Transcript -Path "C:\transcripts\transcript.txt"

at the end of your script use Stop-Transcript

2
  • Nice, This indeed captures all output in a file without redirecting; so console still displays everything. Commented Feb 5, 2020 at 21:41
  • This did not work for me when running MSBuild in Developer PowerShell (Windows PowerShell).
    – pooya13
    Commented Mar 17 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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