# PowerShell scripts related to Diagnostic Data Viewer

Diagnostic data on Windows refers to the information collected and sent by the operating system to Microsoft to help identify and troubleshoot issues, improve system performance, and enhance user experiences. Using Diagnostic Data Viewer, users can view and analyze diagnostic data collected by the operating system.

With the provided PowerShell scripts, you can check the current status of Diagnostic Data Viewer, disable or enable diagnostic data viewing, and delete diagnostic data history when needed. Additionally, you can retrieve the diagnostic data store capacity and control how much diagnostic data history can be shown. Admins can deploy these scripts to multiple devices using Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) feature.

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

PowerShell script 
------------------

Use the following script to install the **Nuget** and **Microsoft.DiagnosticDataViewer** module on the Windows device. These modules are required to execute the other scripts described in the document.

Script to install diagnostic data module on Windows

try { if(Get-PackageProvider | Where-Object {$\_.Name -eq "Nuget"}) { "Nuget Module already exists" } else { "Installing nuget module" Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force } Install-Module Microsoft.DiagnosticDataViewer -Force } catch { Write-Output $\_.Exception.Message }

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

  try

{

if(Get-PackageProvider | Where-Object {$\_.Name -eq "Nuget"})

{

"Nuget Module already exists"

}

else

{

"Installing nuget module"

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

}

Install-Module Microsoft.DiagnosticDataViewer -Force

}

catch {

Write-Output $\_.Exception.Message

}

   

 

  

### Check diagnostic data status 

The command fetches the current state of Diagnostic Data Viewer and checks whether it is enabled or disabled on the device.

Script to install diagnostic data module on Windows

Get-DiagnosticDataViewingSetting

   1

  Get-DiagnosticDataViewingSetting

   

 

  

[![Get the current status of diagnostic data viewing](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/08/Fetch-current-state-of-diagnostic-data-viewing.png "Fetch current state of diagnostic data viewing")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/08/Fetch-current-state-of-diagnostic-data-viewing.png)

### Disable and delete diagnostic data 

Disable diagnostic data viewing and delete all diagnostic data history on the device using the following command.

Script to delete diagnostic data history on Windows

Disable-DiagnosticDataViewing

   1

  Disable-DiagnosticDataViewing

   

 

  

[![Disable diagnostic data viewing and delete all diagnostic data history](https:2023/08/Diagnostic-data-viewing-is-disabed.png "Diagnostic data viewing is disabled")](https:2023/08/Diagnostic-data-viewing-is-disabed.png)

### Enable diagnostic data 

Enable diagnostic data viewing on the device using the following command. Once enabled, the device will log every diagnostic data event sent to Microsoft.

Script to enable diagnostic data viewing on Windows

Enable-DiagnosticDataViewing

   1

  Enable-DiagnosticDataViewing

   

 

  

[![Diagnostic data viewing is enabled on the device](https:2023/08/Diagnostic-data-viewing-is-enabled.png "Diagnostic data viewing is enabled")](https:2023/08/Diagnostic-data-viewing-is-enabled.png)

### Fetch the diagnostic store capacity 

The command fetches the current diagnostic store capacity, showing the size in megabytes and time in days. The default capacity is 1024 MB and 30 days. When either limit is met (whichever happens first), the diagnostic data is removed in a first-in-first-out manner. For instance, if the size limit is 2GB and the time limit is 45 days, the oldest entry will be discarded once the storage reaches 2GB or the oldest event is 30 days old (whichever occurs first).

To obtain the configured store capacity (in megabytes) of the diagnostic store, execute the following command:

Script to fetch diagnostic store size capacity on Windows

Get-DiagnosticStoreCapacity -Size

   1

  Get-DiagnosticStoreCapacity -Size

   

 

  

[![Get the diagnostic store size capacity](https:2023/08/Fetch-diagnostic-store-size-capacity.png "Fetch diagnostic store size capacity")](https:2023/08/Fetch-diagnostic-store-size-capacity.png)

To obtain the configured store capacity (in hours) of the diagnostic store, execute the following command:

Script to fetch diagnostic store time capacity on Windows

Get-DiagnosticStoreCapacity -Time

   1

  Get-DiagnosticStoreCapacity -Time

   

 

  

[![Get the diagnostic store time capacity](https:2023/08/Fetch-diagnostic-store-time-capacity.png "Fetch diagnostic store time capacity")](https:2023/08/Fetch-diagnostic-store-time-capacity.png)

### Set the diagnostic store capacity 

This command allows you to configure the maximum amount of diagnostic data history that can be displayed. You can set the size cap in megabytes and the time cap in days. When either cap is reached, diagnostic data history is removed in a first-in-first-out order.

To set the size capacity of the diagnostic store to 512 (in megabytes), execute the following command:

Script to set diagnostic store size capacity on Windows

Set-DiagnosticStoreCapacity -Size 512

   1

  Set-DiagnosticStoreCapacity -Size 512

   

 

  

[![Set a custom diagnostic size capacity](https:2023/08/Diagnostic-size-capacity-is-set-to-512-MB.png "Diagnostic size capacity is set to 512 MB")](https:2023/08/Diagnostic-size-capacity-is-set-to-512-MB.png)

To set the time capacity of the diagnostic store to 12 (in hours), execute the following command:

Script to set diagnostic store time capacity on Windows

Set-DiagnosticStoreCapacity -Time 12

   1

  Set-DiagnosticStoreCapacity -Time 12

   

 

  

[![Set a custom diagnostic time capacity](https:2023/08/Diagnostic-time-capacity-is-set-to-12-hours.png "Diagnostic time capacity is set to 12 hours")](https:2023/08/Diagnostic-time-capacity-is-set-to-12-hours.png)

### Fetch diagnostic data history 

This command retrieves the historical Windows diagnostic data uploaded by this machine. The amount of available historical data is constrained by the configurations of the diagnostic data store. For modifications in the diagnostic store capacity, refer to [Set the diagnostic store capacity](#set-the-diagnostic-store-capacity).

The following command will retrieve all diagnostic events occurred within the time frame of 20 and 6 hours before the script execution time.

Script to fetch diagnostic events between a period of time

Get-DiagnosticData -StartTime (Get-Date).AddHours(-20) -EndTime (Get-Date).AddHours(-6)

   1

  Get-DiagnosticData -StartTime (Get-Date).AddHours(-20) -EndTime (Get-Date).AddHours(-6)

   

 

  

[![Get all the diagnostic events occurred between a period of time](https:2023/08/Fetch-diagnostic-events-between-a-period-of-time.png "Fetch diagnostic events between a period of time")](https:2023/08/Fetch-diagnostic-events-between-a-period-of-time.png)

The following command will retrieve the first (oldest) diagnostic event since the previous ‘n’ days.

Script to fetch oldest diagnostic event since previous n days

Get-DiagnosticData -StartTime (Get-Date).AddDays(-1) -RecordCount 1

   1

  Get-DiagnosticData -StartTime (Get-Date).AddDays(-1) -RecordCount 1

   

 

  

The parameter **-RecordCount** specifies the maximum number of events to retrieve.

[![Get the oldest diagnostic event since the previous ‘n’ days](https:2023/08/Oldest-diagnostic-event-since-the-previous-n-days.png "Oldest diagnostic event since the previous 'n' days")](https:2023/08/Oldest-diagnostic-event-since-the-previous-n-days.png)

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.