Share via

Problem with path in a Powershell script

Kaplan, Andrew H 226 Reputation points
2026-02-05T15:41:02.0866667+00:00

Hello.

I am trying to export the contents of a Custom View from the Event Viewer to a csv file. The first part of the process, for now, involves my interactively outputting to an xml file. The file is sent to the C:\Logs folder.

The next part is a Powershell script designed to output the contents of the xml file to csv format. The name of the script is mghroswl3loginsoutput.ps1, and the code is the following:

# Path to your exported Custom View XML

$xmlPath = "C:\Logs\mghroswl3logins.xml"

# Load the XML filter

[xml]$xmlFilter = Get-Content $xmlPath

# Get events matching the filter

$events = Get-WinEvent -FilterXml $xmlFilter

# Output to CSV

$events |

Select-Object TimeCreated, Id, LevelDisplayName, ProviderName, Message |

Export-Csv "C:\Logs\mghroswl3logins.csv" -NoTypeInformation -Encoding UTF8

The script is located in the C:\Logs folder. The command syntaxes that I have tried are the following:

.\mghroswl3loginsoutput.ps1

.\mghroswl3loginsoutput.ps1 mghroswl3logins.xml

The error message that I am getting is shown below:

Get-Content : Cannot find path 'C:\Logs\C\Logs\mghroswl3logins.xml' because it does not exist.

At C:\Logs\mghroswl3loginsoutput.ps1:5 char:19

+ [xml]$xmlFilter = Get-Content $xmlPath

+ ~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : ObjectNotFound: (C:\Logs\C\Logs\mghroswl3logins.xml:String) [Get-Content], ItemNotFoundE

xception

+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Get-WinEvent : Cannot bind argument to parameter 'FilterXml' because it is null.

At C:\Logs\mghroswl3loginsoutput.ps1:8 char:35

+ $events = Get-WinEvent -FilterXml $xmlFilter

+ ~~~~~~~~~~

+ CategoryInfo : InvalidData: (:) [Get-WinEvent], ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetWinEvent

Command

The path in the error message shows a wrong path. How can I correct it?

Windows for business | Windows Server | Networking | Other
0 comments No comments

6 answers

Sort by: Most helpful
  1. MotoX80 37,686 Reputation points
    2026-02-06T13:34:42.6633333+00:00

    I don't see anything obviously wrong with your script.

    Are you using Powershell version 5.1 or the cross platform pwsh.exe?

    I added some additional troubleshooting statements. What output does this script produce?

    # Define our files. 
    $xmlPath = "C:\Logs\mghroswl3logins.xml"
    $CsvPath = "C:\Logs\mghroswl3logins.csv" 
    
    "Powershell version is {0}" -f $PSVersionTable.PSVersion
    
    # Process Custom View XML
    "Using XML file {0}" -f $xmlPath
    if (Test-Path $xmlPath) {
        "I can see the file."
    } else {
        "File not found!!!"
        $folder = Split-Path $xmlPath            # pick off the folder name. 
        "Your XML file is in this folder {0}" -f $folder
        if (Test-Path $folder) {
            "I can see the folder."
            $files = Get-ChildItem $folder -File
            "I can see that you have {0} files in that folder." -f $files.count
        } else {
            "Folder not found!!!!!!!!"
        }
        return
    } 
    
    # Load the XML filter
    [xml]$xmlFilter = Get-Content  $xmlPath
    
    # Get events matching the filter
    $events = Get-WinEvent -FilterXml $xmlFilter
    "We parsed {0} events." -f $events.count
    
    # Output to CSV
    $events |
    Select-Object TimeCreated, Id, LevelDisplayName, ProviderName, Message |
    Export-Csv $CsvPath -NoTypeInformation -Encoding UTF8
    
    

    Was this answer helpful?


  2. Kaplan, Andrew H 226 Reputation points
    2026-02-05T16:30:22.95+00:00

    Hello Jason.

    I modified the script to read as follows:

    # Path to your exported Custom View XML

    # $xmlPath = "C:\Logs\mghroswl3logins.xml"

    param([string]$xmlFile ="C:\Logs\mghroswl3logins.xml")

    # Load the XML filter

    [xml]$xmlFilter = Get-Content $xmlFile

    # Get events matching the filter

    $events = Get-WinEvent -FilterXml $xmlFilter

    # Output to CSV

    $events |

    Select-Object TimeCreated, Id, LevelDisplayName, ProviderName, Message |

    Export-Csv "C:\Logs\mghroswl3logins.csv" -NoTypeInformation -Encoding UTF8

    However, the same error message is continuing to appear.

    Was this answer helpful?

    0 comments No comments

  3. Jason Nguyen Tran 17,755 Reputation points Independent Advisor
    2026-02-05T16:24:29.7433333+00:00

    Hi Kaplan, Andrew H,

    I can see why the duplicate path (C:\Logs\C\Logs\...) would cause confusion. The issue arises because the script is already hardcoding the full path to the XML file ($xmlPath = "C:\Logs\mghroswl3logins.xml"), but when you run the script with an argument like .\mghroswl3loginsoutput.ps1 mghroswl3logins.xml, PowerShell interprets that argument as an additional path and concatenates it, resulting in the incorrect C:\Logs\C\Logs\... path.

    To fix this, you have two options:

    1. Run the script without arguments since the $xmlPath variable already points to the correct file. Simply use:

    .\mghroswl3loginsoutput.ps1
    

    This will work as long as the XML file is located at C:\Logs\mghroswl3logins.xml.

    2. Modify the script to accept a parameter if you want flexibility. For example:

    param([string]$xmlFile = "C:\Logs\mghroswl3logins.xml")  
    [xml]$xmlFilter = Get-Content $xmlFile
    

    Then you can run:

    .\mghroswl3loginsoutput.ps1 C:\Logs\mghroswl3logins.xml
    

    This ensures the path is passed correctly and avoids duplication.

    Also, please double-check that the XML file exists in the C:\Logs folder and that the filename matches exactly, including extension.

    You can try and let me know how it goes. If you find this answer helpful, please consider clicking Accept Answer so I know your concern has been resolved.

    Jason.

    Was this answer helpful?

    0 comments No comments

  4. Kaplan, Andrew H 226 Reputation points
    2026-02-05T16:23:04.4533333+00:00

    Hello. I confirmed the syntax and pathnames are correct. As suggested, I ran the 'Set-Location C:\Logs' command. After that, I reran the script, and the same error message appeared on-screen.

    Was this answer helpful?

    0 comments No comments

  5. Q&A Assist
    2026-02-05T15:41:11.87+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The error you're encountering indicates that the script is trying to access a path that is incorrectly formed. Specifically, the error message shows C:\Logs\C\Logs\mghroswl3logins.xml, which suggests that the path is being concatenated incorrectly, leading to the duplication of C:\Logs.

    To resolve this issue, ensure that the $xmlPath variable is correctly defined and does not have any unintended modifications. Here are a few steps you can take to troubleshoot and fix the issue:

    1. Check the Definition of $xmlPath: Make sure that the line defining $xmlPath is exactly as follows:
         $xmlPath = "C:\Logs\mghroswl3logins.xml"
      
      Ensure there are no additional characters or concatenations that could alter the path.
    2. Use Absolute Paths: When calling the script, ensure that you are using the correct absolute path to the XML file. Since the script is located in C:\Logs, the path to the XML file should remain as C:\Logs\mghroswl3logins.xml.
    3. Run the Script from the Correct Directory: Make sure you are running the script from the C:\Logs directory. You can navigate to this directory in PowerShell using:
         Set-Location C:\Logs
      
      Then run the script with:
         .\mghroswl3loginsoutput.ps1
      
    4. Check for Typos: Ensure there are no typos in the script or the file names.

    By following these steps, you should be able to correct the path issue and successfully run your PowerShell script without encountering the path not found error.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.