# $Error\[0\] is null

**URL:** <https://forums.powershell.org/t/error-0-is-null/25074>\
**Category:** PowerShell Help\
**Created:** [November 27, 2024, 5:51pm UTC](https://forums.powershell.org/t/error-0-is-null/25074 "2024-11-27T17:51:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [November 27, 2024, 5:51pm UTC](https://forums.powershell.org/t/error-0-is-null/25074/1 "2024-11-27T17:51:21Z")

</div>

I maintain a little module of CLI tools for my team at work and have a behavior i can’t account for.  
I keep separate .ps1 files for all function definitions, and then “build” them in to a monolithic .psm1 file before publishing the module.  
I say this because when I’m writing and testing the function, it’s a single file and I redefine the function in my terminal (VS Code) whenever there is a change and test.  
I’ve been doing error handling like this quite a bit:

```Powershell
try {
    #some code
} catch {
    Write-Warning $Error[0]
}

```

When I do this during development I get what I wanted:  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/7/7fa345ca639b37c005c424b7d75d8a3a52eaeb61.png)  
However, the same circumstances leading to the error behave differently once it’s in the module.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/0/0997c091adcba3c3dbd588c8ceae0c1788a9dc8b.png)

When this has happened in the past I know i’ve gone back and tried changing `$Error[0]` to `$Error[0].Message` or something like that, and in testing it works, but then in the module it might occur again. Most of the time the errors are few and far between so it’s hard to test.  
But this recent function, Get-LockEvent, will always generate an error if the target computer doesn’t have any Event ID 4800 or 4801 in the Security log.

My question is: what’s happening with the automatic $Error variable when my code is executing from my module instead of a single function definition?

Here is the full function definition i’m working with:

```Powershell
function Get-LockEvent {
    <#
    .SYNOPSIS
    Retrieve logs from Windows Events pertaining to computer lock and unlock.
    .DESCRIPTION
    Easier to remember wrapper for Get-WinEvent and a filterhashtable for getting event ID 4800 and 4801 from the Windows Security log.
    .PARAMETER ComputerName
    Can specify a remote computer to pull logs from
    .PARAMETER OutputType
    By default EventLogRecord objects are returned from Get-WinEvent. You can specify PSObject for output type and the event records will be converted to XML, and then turned in
    to Powershell objects from there for easier reading/exporting/filtering.
    .PARAMETER TimeFrame
    By default it will filter for events occuring today. You can specify "All", "Today" or "LastHour" and Get-WinEvent will filter accordingly. 
    #>
    [cmdletbinding()]
    Param (
        [Parameter(Mandatory=$false,Position=0)]
        [String]$ComputerName,
        [Parameter(Mandatory=$false,Position=1)]
        [ValidateSet("Default","PSObject")]
        [String]$OutputType = "Default",
        [Parameter(Mandatory=$false,Position=2)]
        [ValidateSet("All","Today","LastHour")]
        [String]$TimeFrame = "Today"
    )

    $StartTime = switch ($TimeFrame) {
        "Today" { Get-Date $((Get-Date).ToShortDateString()) }
        "All" { $false }
        "LastHour" { (Get-Date).AddHours(-1) }
    }

    $EventHT = @{
        FilterHashTable = @{
                LogName = "Security"
                Id = 4800,4801
        }
        ErrorAction = "Stop"
    }

    if ($StartTime) {
        $EventHT.FilterHashTable.Add("StartTime",$StartTime)
    }

    if ($ComputerName) {
        $EventHT.Add("ComputerName",$ComputerName)
    }

    try {
        switch ($OutputType) {
            "Default" {
                Get-WinEvent @EventHT
            }
            "PSObject" {
                $Events = Get-WinEvent @EventHT
                Foreach ($Event in $Events) {
                    $XmlEvent = [xml]$Event.ToXml()
                    $EventType = switch ($XmlEvent.Event.System.EventID) {
                        "4800" {"Locked"}
                        "4801" {"Unlocked"}
                    }
                    [PSCustomObject]@{
                        PSTypeName = "LockEvent"
                        DateTime = Get-Date $XmlEvent.event.System.timecreated.systemtime -Format 'MM/dd/yy HH:mm:ss'
                        EventID = $XmlEvent.Event.System.EventID
                        EventType = $EventType
                        TargetUserSID = $XmlEvent.Event.EventData.Data.'#text'[0]
                        TargetUserName = $XmlEvent.Event.EventData.Data.'#text'[1]
                        TargetDomainName = $XmlEvent.Event.EventData.Data.'#text'[2]
                        SessionId = $XmlEvent.Event.EventData.Data.'#text'[4]
                    }
                }
            }
        } 
    } catch {
        Write-Warning $Error[0]
    }
}

```

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [November 27, 2024, 7:17pm UTC](https://forums.powershell.org/t/error-0-is-null/25074/2 "2024-11-27T19:17:27Z")

</div>

I wrote a basic module that only returns $Error.Count. Looks like $Error in a module is different than the current session $Error

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/5/5849b41801f363e1d7bc8e68fb48c9b04355dc82.png)

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [November 27, 2024, 7:31pm UTC](https://forums.powershell.org/t/error-0-is-null/25074/3 "2024-11-27T19:31:11Z")

</div>

I found that in my Modules, to pass errors back to a calling script, I had to reference:

```auto
$Global:Error

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 27, 2024, 8:40pm UTC](https://forums.powershell.org/t/error-0-is-null/25074/4 "2024-11-27T20:40:55Z")

</div>

Why wouldn’t you use `$_` in the catch block? I do `$_.exception.message`

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [November 28, 2024, 12:30am UTC](https://forums.powershell.org/t/error-0-is-null/25074/5 "2024-11-28T00:30:28Z")

</div>

yeah it’s a good question that I don’t have an answer for. I think I picked up the `$Error[0]` behavior from some example code along the way, and since it’s works when i’m authoring the .ps1 file I just didn’t notice. I will test with the current object variable now.

EDIT: switching the code to use `$_` like @krzydoug suggested did the trick. It now processes Write-Warning both in VS Code, and in the terminal when using the compiled module.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 28, 2024, 4:14am UTC](https://forums.powershell.org/t/error-0-is-null/25074/6 "2024-11-28T04:14:36Z")

</div>

According to [SeeminglyScience](https://github.com/seeminglyscience) the $error variable is explicitly empty in module scope. He just happened to be talking about it in the powershell discord. He also said $error is best used by the interactive user, not in a script.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [December 2, 2024, 4:22pm UTC](https://forums.powershell.org/t/error-0-is-null/25074/7 "2024-12-02T16:22:36Z")

</div>

> [@krzydoug](#):
>
> He also said $error is best used by the interactive user, not in a script.

In my larger scripts, I use $Global:Error in my Modules to grab the exact line of code the script failed on. Is there another way to get this without $Error?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [January 1, 2025, 4:23pm UTC](https://forums.powershell.org/t/error-0-is-null/25074/8 "2025-01-01T16:23:23Z")

</div>

Closed on January 1, 2025, 4:23pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
