Issue Mapping User's Home Drive in Windows 10
<p>In the environment that I work in, sometimes the user's Active Directory (AD) home drive is not mapped when they logon because they are not connected to the network.  Later, when they do connect to the network over VPN they need to be able to map their AD home drive.  I've created a script in PowerShell to do that but, on Windows 10 I get the error, " Object reference not set to an instance of an object." And the script fails to map the user's home drive.  If I run the same script on Windows 7 it works without issue.  Any help with this issue would be greatly appreciated.</p>

<pre><code>Param (
    [switch] $verbose,
    [switch] $whatif
)

Function Write-Event{
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [Parameter(Mandatory=$true)]
        $Message,
        [parameter(Mandatory=$FALSE)]
        [String[]] $LogName = 'Application',
        [String[]] $Source = "WSH",
        [String[]] $EntryType = "Information",
        [Int] $EventId = 4  
    )
    $tab= [char]9
    Write-Verbose "$tab In the function to write the event to the log"
    TRY {
        Write-Verbose "$tab Using the Wscript.shell COM object to write the event"
        $WShell = New-Object -ComObject WScript.Shell
        IF ($WhatIf){
            Write-output "What if: Performing operation Writing message to the $LogName with Event ID $EventId and message, $Message"
        } Else {
            $WShell.LogEvent($EventId, $Message) &gt; $null
            Write-Verbose "$tab Info: Successfully wrote, $Message, to $LogName log"    
        }

    }
    CATCH {
        Write-Warning "$tab Error writing, $Message, to $LogName log"
    }
    Write-Verbose "$tab End of function to write the event to the log"
}
Function Get-ADUserProperties {
    #Requires -Version 3.0
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [parameter(Mandatory=$FALSE)]
        [String] $Username = [Environment]::UserName    
    )

    $DateNow = Get-Date
    Write-verbose "$DateNow - Getting Active Directory User Account information"
    Write-Event -Message "$DateNow - Getting Active Directory User Account information"

    # Get the user accounts AD properties       
    Try 
    {
        Write-verbose "Checking to see if User, $Username, can be found within the domain ..."
        $strFilter = "(&amp;(objectClass=user)(name=$Username))"
        # Connect to the domain and create search object
        $objDomain = New-Object System.DirectoryServices.DirectoryEntry
        $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objSearcher.SearchRoot = $objDomain
        $objSearcher.PageSize = 15000
        $objSearcher.Filter = $strFilter
        $objSearcher.SearchScope = "Subtree"
        # Search for System
        $AllObj = $objSearcher.FindAll() 
        $ADUser = $AllObj.Properties

        Return $ADUser
    }
    CATCH 
    {
        $DateNow = Get-Date
        Write-Warning -Message "$DateNow - Error: $Username could not be found in AD"
        Return $null
    }

}

Function Set-HomeDrive {
    #Requires -Version 3.0  
    &lt;#
        .SYNOPSIS
        Script to map a users home folder
        .DESCRIPTION
        The script will check to see if the user running it is found within  Active directory. If the user does exist it will get the AD properties for the user.
        Once the AD properties are known it will check to see if the home folder and path exist and are mapped.
        If they are already mapped it will open explorer.exe to that location.
        If they are not mapped, it will map the folder and open explorer.exe to that location.

        .PARAMETER CRQ
        Takes the CRQ number as input.  Should be a string or integer.
        .PARAMETER Name
        Takes a string as input, this should not contain any spaces or special characters.  Will be part of the log file name.

        .EXAMPLE
        C:\PS&gt; .\MapHomeDrive.ps1
        This will map the users Active Directory Home drive letter to their home share 

        .INPUTS
        None
        .OUTPUTS
        None    

    #&gt;      
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [parameter(Mandatory=$false)]
            [String[]] $CRQ = '19175-10468',
            [String[]] $Name = 'MapHomeDrive'   
    )
    Begin {
        $ErrorActionPreference = "Stop"
        Write-Output -InputObject "`n"
        $DateNow = Get-Date
        Write-Output "$DateNow - Preparing Environment to Map Home Drive"
        $strCompName = $env:computername ; $strWinDir = $env:windir
        $quote = [char]34 ; $tab= [char]9 ; $plus=[char]43 ; $LOGFILE=$null  ; $text = $null ; $Color = $null
        Write-Verbose -Message "Outputting all initial parameters: `n $tab $tab CRQ: $CRQ `n $tab $tab Name: $Name `n"      

    }
    Process {

        $DateNow = Get-Date
        Write-verbose "$DateNow - $Name script started running"
        Write-Event -Message "$DateNow - $Name script started running"
        # Get the user accounts AD properties
        $ADUserProps = Get-ADUserProperties
        IF ($ADUserProps -ne $null) 
        {
            # Set variables for the user's home folder and drive
            $HomeFolder = $ADUserProps.homedirectory
            $HomeDrive = $ADUserProps.homedrive             

            IF (($HomeFolder -eq $null) -or ($HomeDrive -eq $null)) 
            {
                Write-Warning -Message "Error: $Username does not have a home directory or home drive in AD, exiting script" 
                Write-Event -Message "Error: $Username does not have a home directory or home drive in AD, exiting script" -EventID 1               
            }
            ELSE 
            {
                IF ($HomeDrive -Match ":"){$HomeDrive1 = $HomeDrive -replace ".$"} else {$HomeDrive1 = $HomeDrive}          
                # Get the logical disk for the home drive
                $TestHomeDrive = Get-WMIObject Win32_LogicalDisk -Property *|Where-Object {($_.DeviceID -eq $HomeDrive)}
                # Check to see if the drive is already mapped
                IF ($TestHomeDrive -eq $null) 
                {
                    Write-Verbose "Info: The drive, $HomeDrive1, is NOT already mapped, mapping home drive"
                    Write-Event -Message "Info: The drive, $HomeDrive1, is NOT already mapped, mapping home drive" -EventID 2
                    $net = New-Object -ComObject WScript.Network
                   Write-Verbose "$net"
                    TRY 
                    {                       
                        IF ($WhatIf){
                            Write-output "What if: Performing operation mapping, $HomeFolder to $HomeDrive"
                        } Else {
                            $net.MapNetworkDrive($HomeDrive, $HomeFolder, $true)
                        }                       
                    }
                    CATCH {
                        Write-Error -Message "Unable to map $HomeFolder to $HomeDrive  - $_"
                    }
                }
                ELSEIF ($TestHomeDrive.ProviderName -eq $HomeFolder) 
                {
                    Write-Verbose "Info: The drive, $HomeDrive1, is already mapped to $HomeFolder."
                    Write-Event -Message "Info: The drive, $HomeDrive1, is already mapped to $HomeFolder." -EventID 2
                } 
                ELSE 
                {
                    Write-Verbose "$$$$You should not be here"
                }

            }               

        }
    ELSE 
    {
        Write-Warning -Message "Error: $Username could not be found in AD, the home drive will not be mapped, exiting script" 
        Write-Event -Message "Error: $Username could not be found in AD, the home drive will not be mapped, exiting script" -EventID 1          
    }

    # Ensure that the script can get to the Home drive and launch explorer to that location
    IF ($HomeDrive -ne $null)   
    {
        IF (test-path $HomeDrive)
        {
            $DateNow = Get-Date
            $MessageText = "$DateNow - Successfully mapped drive $HomeDrive1 to $HomeFolder"
            Write-Event -Message $MessageText -EventID 0
            Write-Output $MessageText
            # Launch Explorer.exe to the Home drive folder
            $EXSwitches = "/e," + $quote + $HomeDrive1 + ":" + $quote
            &amp; "explorer.exe" $EXSwitches
        } 
    }
    ELSE 
    {
        $DateNow = Get-Date
        $MessageText = "$DateNow - Error mapping drive Home Drive to Home Folder"
        Write-Event -Message $MessageText -EventID 1    
        Write-Verbose $MessageText              
    }


    }
    End {
        $DateNow = Get-Date
        Write-Event -Message "$DateNow - $Name script finished running"
        Write-Output "$DateNow - End of script to map home drive"
        Write-Output -InputObject "`n"
    }

 }
IF ($Verbose) {Set-HomeDrive -verbose} ELSEIF ($Whatif) {Set-HomeDrive -whatif} ELSE {Set-HomeDrive }
</code></pre>

<p>The error output shows the error comes from the line of code "IF ($Verbose) {Set-HomeDrive -verbose} ELSEIF ($Whatif) {Set-HomeDrive -whatif} ELSE {Set-HomeDrive }".  When I run the script with the verbose option I get the following output.</p>

<pre><code>VERBOSE: Outputting all initial parameters:
     CRQ: 19175-10468
     Name: MapHomeDrive
VERBOSE: 10/12/2016 12:08:30 - MapHomeDrive script started running
VERBOSE:   In the function to write the event to the log
VERBOSE:   Using the Wscript.shell COM object to write the event
VERBOSE:   Info: Successfully wrote, 10/12/2016 12:08:30 - MapHomeDrive script started running, to Application log
VERBOSE:   End of function to write the event to the log
VERBOSE: 10/12/2016 12:08:30 - Getting Active Directory User Account information
VERBOSE:   In the function to write the event to the log
VERBOSE:   Using the Wscript.shell COM object to write the event
VERBOSE:   Info: Successfully wrote, 10/12/2016 12:08:30 - Getting Active Directory User Account information, to
Application log
VERBOSE:   End of function to write the event to the log
VERBOSE: Checking to see if User, keith.richardson, can be found within the domain ...
VERBOSE: Info: The drive, H, is NOT already mapped, mapping home drive
VERBOSE:   In the function to write the event to the log
VERBOSE:   Using the Wscript.shell COM object to write the event
VERBOSE:   Info: Successfully wrote, Info: The drive, H, is NOT already mapped, mapping home drive, to Application log
VERBOSE:   End of function to write the event to the log
VERBOSE: System.__ComObject
</code></pre>