r/PowerShell • u/WhiskyEchoTango • Feb 27 '26
Question Script not creating the log file
I have a script I am working on that should make a log file, but the script isn't making the file. I'm not very experienced with this, but it works as an independent command.
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param(
[Parameter(Mandatory=$true)]
[string]$CsvPath,
[string]$LogPath = ".\profile-import-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
)
function Write-Log {
param([string]$Message)
$line = "{0} {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Message
$line | Tee-Object -FilePath $LogPath -Append | Out-Null
}
# Connect to Microsoft Graph
Import-Module Microsoft.Graph.Users
$scopes = @("User.ReadWrite.All")
Write-Log "Connecting to Microsoft Graph with scopes: $($scopes -join ', ')"
Connect-MgGraph -Scopes $scopes | Out-Null
2
Upvotes
View all comments
Show parent comment
1
u/evasive_btch Feb 28 '26 edited Feb 28 '26
Did you fix it? I see a pretty straigth-forward way to debug this. You're just writing a file, the only things that should matter are permissions, which the error message will tell you more about.
Open a terminal as admin and do:
Does it work? If not, do you get an Error message? What is that message? I think you are hindering your debugging by adding
| Out-Null. You could also justOut-Fileinstead ofTee-Objectno?Is your user an administrator? If not, yes, administrators can have, and set, permissions for everything, but administrators dont have permissions by default. Especially for other users folders, which by default dont have permissions set for administrators.