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
6
u/Ferenik Feb 27 '26
add a debug to the end to write to screen
Write-Host "Log location is: $LogPath"
check to see where it's actually writing too. might be that the .\ is just dumping it somewhere since if you .\ you're path it will use the relevant context for that.
for example if you run it in ISE the default path is usually C:/{user}/Documents.
1
u/WhiskyEchoTango Feb 28 '26
I should mention that I am running this script with -whatif, before I run it against my tenant.
1
u/Zozorak Feb 27 '26
Are you running from the. Ps1 file? If so, check 'get-executionpolicy'. By default I think it's set to restricted.
If your running without a $logpath parameter it's trying to store it in the directory you're in. Check there. If you're adding a logpath, make sure it's a full path like c:\logs\log.Log
1
u/Exciting_Shoe2095 Feb 27 '26
Hello.
Try using Join-Path when declaring your log path, like below:
$LogPath = Join-Path $PSScriptRoot "profile-import-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
Also... You're declaring your $csvPath param as mandatory. Are you definitely providing it a csv path when running the script?
3
u/dodexahedron Feb 28 '26
An FYI not directly related to the question:
DateTime has built-in format strings that are standard and meant for exactly the things you're doing with dates here.
The main being O, just by itself, as the only content of the format string.
O outputs a full ISO8601 date string, which is the exact format you use on the log lines. You can also use O2 if you want 2 digits of fractional seconds, for example - number is optional and controls that.
If you want it in UTC instead of local, use U instead of O.
If you want it with time offset, use [DateTimeOffset]::Now instead of Get-Date. The format strings are the same.
Here are the rest of the built-ins and some guidance on type choice: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#table-of-format-specifiers
2
u/jeffrey_f Feb 27 '26
ALWAYS use the fully qualified path to the location you want to read or write from/to. If this is running as a scheduled job and as a different user, it won't work as you expect. Use the fully qualified path and make sure you have access to this path.
1
u/dodexahedron Feb 28 '26
Why are you using Tee-Object and piping to null? Seems unnecessary.
1
u/BlackV Feb 28 '26
you are using a relative path here
how are you validating the correct path ?
1
u/hailGunslinger9 Feb 28 '26
I create a folder in the root of the C drive and then add my log there for local troubleshooting. I've been messing with creating the log in the IME temp folder to report back to Intune for remediations recently (based off some wonderful recommendations from r/powershell & intune). I'm not at my PC currently but can post some code in a bit.
6
u/[deleted] Feb 27 '26
[removed] — view removed comment
1
u/[deleted] Feb 27 '26
[removed] — view removed comment