I'm working on a PowerShell script to generate a log file, but it seems to be failing at that. The script runs fine as an independent command, but no log file is created. Here's part of my script: it uses a parameter for the `LogPath` which defaults to the current directory, but I'm not sure where that is when I run it. I've connected to Microsoft Graph as part of the script and included a function for writing logs. Can anyone help me troubleshoot why the log file isn't being created?
5 Answers
Just a heads-up: relying on relative paths is risky. They can produce unexpected results depending on where your script runs from. Always use full paths for logs and files to avoid confusion.
You might want to add a debug line at the end of your script to check where the log file is supposed to be created. Try adding `Write-Host "Log location is: $LogPath"` to see if it prints the expected path. Sometimes the relative path you're using can lead to it being saved in a different directory than you think, especially if you're running it from PowerShell ISE or another context.
After adding that, I confirmed the path: `Log location is: .profile-import-20260227-171019.log`. I ran the script directly using `.script.ps1`, but still no log file.
Make sure that you're running the script properly. You might want to check your execution policy; run `get-executionpolicy` to see if it's set to `Restricted`. If you're not giving a full path for your log, it might be saved in a directory you aren't checking.
I've already set it with `set-executionpolicy -scope process Unrestricted`, but it's still not working.
Consider using `Join-Path` to set your log path. It helps ensure you're combining paths properly. Also, double-check whether you're actually passing a CSV path when you run your script, since it's mandatory but can lead to issues if not provided correctly.
Be cautious with relative paths. It's better to prefix them with `$PSScriptRoot` to ensure your log files go exactly where you intend them to. Otherwise, your logs might end up in unexpected locations.
I switched to using `$LogPath = Join-Path $PSScriptRoot "profile-import-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"`, and it confirmed the log location properly, but the file is still missing.

I tried that approach too, specifying the full path as `C:Users[PATH]DocumentsPowerShellprofile-import-$(Get-Date -Format 'yyyyMMdd-HHmmss').log`. The log location message appears correct, but there’s still no file.