Powershell Can';无法在CSV中写入空字符串

Powershell Can';无法在CSV中写入空字符串,powershell,csv,active-directory,Powershell,Csv,Active Directory,我想问一下有没有什么问题。如果查询在Active Directory上找不到任何用户,但未按预期工作,我希望在CSV中输出“null” 样本输出: Name UserGiven UserLastName usvalj02adm Juhn Paul Valencia ustalu00 Juhn Paul Valencia usnaic00adm Juhn Paul Valencia usmenm03adm Marion Paul Mendoza usjorv00A

我想问一下有没有什么问题。如果查询在Active Directory上找不到任何用户,但未按预期工作,我希望在CSV中输出“null”

样本输出:

Name UserGiven UserLastName usvalj02adm Juhn Paul Valencia ustalu00 Juhn Paul Valencia usnaic00adm Juhn Paul Valencia usmenm03adm Marion Paul Mendoza usjorv00ADM Vincent Jorge usgols01ADM Vincent Jorge usgedr00 Vincent Jorge usdhaa00adm Vincent Jorge uscrud00adm Don Eduard Cruz uscabg00adm Don Eduard Cruz
使用“
或“
替代$null这是可行的,因为CSV中的字符串不是null它是

对不起,我不清楚您的问题是什么。无法复制。AFAICS在您发布的代码中没有任何内容可以保留上一次迭代的值。
Import-Module ActiveDirectory

Get-Content -Path 'C:\Users.txt' | foreach {
    $User   = Get-ADUser $_
    $Groups = Get-ADPrincipalGroupMembership $User

    if ($User.givenName -eq $null) {
        $Empty = @{
            Name         = $_
            UserGiven    = "null"
            UserLastName = "null"
            #GroupDN      = "null"
        }
        New-Object PSObject -Property $Empty
    } else {
        $Props = @{
            Name         = $_
            UserGiven    = $User.givenName
            UserLastName = $User.surName
            #GroupDN      = "null"
        }
        New-Object PSObject -Property $Props
    }
    # One CSV line for each user/group 
} | Export-Csv -Path 'C:\UserGroups.csv' -NoTypeInfo