vendredi 19 novembre 2021

(POWERSHELL) WINDOWS 10 - Supprimer les applications du Windows Store pour alléger les configurations

 # Functions

function Write-LogEntry {

    param(

        [parameter(Mandatory=$true, HelpMessage="Value added to the RemovedApps.log file.")]

        [ValidateNotNullOrEmpty()]

        [string]$Value,


        [parameter(Mandatory=$false, HelpMessage="Name of the log file that the entry will written to.")]

        [ValidateNotNullOrEmpty()]

        [string]$FileName = "RemovedApps.log"

    )

    # Determine log file location

    $LogFilePath = Join-Path -Path $env:windir -ChildPath "Temp\$($FileName)"


    # Add value to log file

    try {

        Out-File -InputObject $Value -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop

    }

    catch [System.Exception] {

        Write-Warning -Message "Unable to append log entry to RemovedApps.log file"

    }

}


# Get a list of all apps

Write-LogEntry -Value "Starting built-in AppxPackage, AppxProvisioningPackage and Feature on Demand V2 removal process"

$AppArrayList = Get-AppxPackage -PackageTypeFilter Bundle -AllUsers | Select-Object -Property Name, PackageFullName | Sort-Object -Property Name


# White list of appx packages to keep installed

$WhiteListedApps = @(

    "Microsoft.DesktopAppInstaller", 

    "Microsoft.Messaging", 

    "Microsoft.MSPaint",

    "Microsoft.Windows.Photos",

    "Microsoft.StorePurchaseApp",

    "Microsoft.MicrosoftOfficeHub",

    "Microsoft.MicrosoftStickyNotes",

    "Microsoft.WindowsAlarms",

    "Microsoft.WindowsCalculator", 

#    "Microsoft.WindowsCommunicationsApps", # Mail, Calendar etc

    "Microsoft.WindowsSoundRecorder" 

#    "Microsoft.WindowsStore"

)


# Loop through the list of appx packages

foreach ($App in $AppArrayList) {

    # If application name not in appx package white list, remove AppxPackage and AppxProvisioningPackage

    if (($App.Name -in $WhiteListedApps)) {

        Write-LogEntry -Value "Skipping excluded application package: $($App.Name)"

    }

    else {

        # Gather package names

        $AppPackageFullName = Get-AppxPackage -Name $App.Name | Select-Object -ExpandProperty PackageFullName -First 1

        $AppProvisioningPackageName = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like $App.Name } | Select-Object -ExpandProperty PackageName -First 1


        # Attempt to remove AppxPackage

        if ($AppPackageFullName -ne $null) {

            try {

                Write-LogEntry -Value "Removing AppxPackage: $($AppPackageFullName)"

                Remove-AppxPackage -Package $AppPackageFullName -ErrorAction Stop | Out-Null

            }

            catch [System.Exception] {

                Write-LogEntry -Value "Removing AppxPackage '$($AppPackageFullName)' failed: $($_.Exception.Message)"

            }

        }

        else {

            Write-LogEntry -Value "Unable to locate AppxPackage: $($AppPackageFullName)"

        }


        # Attempt to remove AppxProvisioningPackage

        if ($AppProvisioningPackageName -ne $null) {

            try {

                Write-LogEntry -Value "Removing AppxProvisioningPackage: $($AppProvisioningPackageName)"

                Remove-AppxProvisionedPackage -PackageName $AppProvisioningPackageName -Online -ErrorAction Stop | Out-Null

            }

            catch [System.Exception] {

                Write-LogEntry -Value "Removing AppxProvisioningPackage '$($AppProvisioningPackageName)' failed: $($_.Exception.Message)"

            }

        }

        else {

            Write-LogEntry -Value "Unable to locate AppxProvisioningPackage: $($AppProvisioningPackageName)"

        }

    }

}


# White list of Features On Demand V2 packages

Write-LogEntry -Value "Starting Features on Demand V2 removal process"

$WhiteListOnDemand = "NetFX3|Tools.Graphics.DirectX|Tools.DeveloperMode.Core|Language|Browser.InternetExplorer|ContactSupport|OneCoreUAP|Media.WindowsMediaPlayer"


# Get Features On Demand that should be removed

try {

    $OSBuildNumber = Get-WmiObject -Class "Win32_OperatingSystem" | Select-Object -ExpandProperty BuildNumber


    # Handle cmdlet limitations for older OS builds

    if ($OSBuildNumber -le "16299") {

        $OnDemandFeatures = Get-WindowsCapability -Online -ErrorAction Stop | Where-Object { $_.Name -notmatch $WhiteListOnDemand -and $_.State -like "Installed"} | Select-Object -ExpandProperty Name

    }

    else {

        $OnDemandFeatures = Get-WindowsCapability -Online -LimitAccess -ErrorAction Stop | Where-Object { $_.Name -notmatch $WhiteListOnDemand -and $_.State -like "Installed"} | Select-Object -ExpandProperty Name

    }


    foreach ($Feature in $OnDemandFeatures) {

        try {

            Write-LogEntry -Value "Removing Feature on Demand V2 package: $($Feature)"


            # Handle cmdlet limitations for older OS builds

            if ($OSBuildNumber -le "16299") {

                Get-WindowsCapability -Online -ErrorAction Stop | Where-Object { $_.Name -like $Feature } | Remove-WindowsCapability -Online -ErrorAction Stop | Out-Null

            }

            else {

                Get-WindowsCapability -Online -LimitAccess -ErrorAction Stop | Where-Object { $_.Name -like $Feature } | Remove-WindowsCapability -Online -ErrorAction Stop | Out-Null

            }

        }

        catch [System.Exception] {

            Write-LogEntry -Value "Removing Feature on Demand V2 package failed: $($_.Exception.Message)"

        }

    }    

}

catch [System.Exception] {

    Write-LogEntry -Value "Attempting to list Feature on Demand V2 packages failed: $($_.Exception.Message)"

}


# Complete

Write-LogEntry -Value "Completed built-in AppxPackage, AppxProvisioningPackage and Feature on Demand V2 removal process"


ALTERNATIVE très intéressante  : Windows 10 Debloater

https://github.com/Sycnex/Windows10Debloater

jeudi 18 novembre 2021

Exchange (Office 365) & Powershell - Gestion des groupes de distribution dynamiques

 1) Préalable pour se connecter à Exchange (Office 365)

Install-Module -Name ExchangeOnlineManagement

(si ce n'est pas déjà fait)

2) Se connecter à Exchange (Office 365)

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName moncompteadmin@mondomaine.com


Voir l'ensemble des boites aux lettres appartenant au groupe de distribution dynamique

Get-Recipient -Filter (Get-DynamicDistributionGroup “groupedynamique@mondomaine.com”).RecipientFilter


Créer un groupe dynamique contenant l'ensemble des boites mails des utilisateurs 

Set-DynamicDistributionGroup -Identity groupedynamique@mondomaine.com -RecipientFilter {(RecipientType -eq 'UserMailbox') -and (-not(RecipientTypeDetails -eq 'SharedMailbox')) -and (-not(RecipientTypeDetails -eq 'RoomMailbox')) -and (-not(RecipientTypeDetails -eq 'MailContact')) -and (-not(RecipientTypeDetails -eq 'DynamicDistributionGroup')) -and (-not(RecipientTypeDetails -eq 'EquipmentMailbox')) }

Par ces paramètres on élimine :
  • Les boites aux lettres partagées
  • Les boites aux lettres de salles
  • Les boites aux lettres des contacts
  • Les boites aux lettres de groupes de distribution dynamiques
  • Les boites aux lettres d'équipement

Connaitre les filtres appliqués sur un groupe de distribution dynamique

Get-DynamicDistributionGroup -Identity groupedynamique@mondomaine.com | fl Name,RecipientFilter