———Draft

# Install these modules before importing if you haven't already!
# Install-Module Microsoft.Graph.Beta.Devicemanagement
# Install-Module Microsoft.Graph.Beta.Users

Import-Module Microsoft.Graph.Beta.Devicemanagement
Import-Module Microsoft.Graph.Beta.Users

Connect-MgGraph -Scopes "User.Read.All", "DeviceManagementManagedDevices.Read.All" -NoWelcome

Write-Output "Fetching device information..."

# Get multiple device names as input
$deviceNames = Get-Content -Path "devices.txt"  # Replace with your input method (e.g., user prompt)

$allUsersList = @()

foreach ($deviceName in $deviceNames) {
  $device = Get-MgBetaDeviceManagementManagedDevice -Filter "contains(deviceName,'$deviceName')"

  if ($device) {
    $lastUsers = $device.UsersLoggedOn

    foreach ($user in $lastUsers) {
      $lastLogon = $user.LastLogOnDateTime
      $userObject = New-Object PSObject -Property @{
        DeviceName = $deviceName
        UserId = $user.UserId
        DisplayName = (Get-MgBetaUser -UserId $user.UserId).DisplayName
        LastLoggedOnDateTime = $lastLogon
      }
      $allUsersList += $userObject
    }
  } else {
    Write-Warning "Device '$deviceName' not found."
  }
}

# Export results to Excel
$allUsersList | Export-Excel -Path "LastLoggedOnUsers.xlsx" -AutoSize

Disconnect-MgGraph
------------

# Install these modules before importing if you haven't already!
# Install-Module Microsoft.Graph.Beta.Devicemanagement
# Install-Module Microsoft.Graph.Beta.Users

Import-Module Microsoft.Graph.Beta.Devicemanagement
Import-Module Microsoft.Graph.Beta.Users

Connect-MgGraph -Scopes "User.Read.All", "DeviceManagementManagedDevices.Read.All" -NoWelcome

Write-Output ""
Write-Output "This script allows you to find the last logged on user(s) of Intune-joined devices."
Write-Output ""

# Get multiple device names as input
$deviceNames = Get-Content -Path "devices.txt"  # Replace with your input method (e.g., user prompt)

Write-Output "Searching for last logged on users for the following devices:"
Write-Output ($deviceNames -join "`n")
Write-Output ""

$allUsersList = @()

foreach ($deviceName in $deviceNames) {
  # Queries for the device that matches the name and sets the variable equal to the Users Logged On
  $device = Get-MgBetaDeviceManagementManagedDevice -Filter "contains(deviceName,'$deviceName')"

  if ($device) {
    $lastUsers = $device.UsersLoggedOn

    $usersList = @()

    foreach ($user in $lastUsers) {
      $lastLogon = $user.LastLogOnDateTime
      $userObject = New-Object PSObject -Property @{
        UserId = $user.UserId
        DisplayName = (Get-MgBetaUser -UserId $user.UserId).DisplayName
        LastLoggedOnDateTime = $lastLogon
      }
      $usersList += $userObject
    }

    # Display results for this device
    Write-Output "Device: $deviceName"
    Write-Output ($usersList | Out-GridView -Title "Last Logged On Users")
    Write-Output ""

    $allUsersList += $usersList  # Add users to a combined list for later display (optional)
  } else {
    Write-Output "Device '$deviceName' not found."
    Write-Output ""
  }
}

# Optionally display a combined list of all users from all devices
if ($allUsersList) {
  Write-Output "All Users (from all devices):"
  Write-Output ($allUsersList | Out-GridView -Title "Last Logged On Users (All Devices)")
}

read-host "Press Enter to close this window"

Disconnect-MgGraph

We will be happy to hear your thoughts

Leave a reply

TechEggs
Logo