Below script is used to delete user profiles based on user logon names
# Delete the Files in Azure file share by taking input value GPN(logon) which is provided in CSV
### Az.Accounts, Az.Resources & Az.Storage modules are required to execute the script.
# To Set TLS1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$path="C:\Temp" ### Path for input & output files
$NTRUsers=Import-Csv $path\NTRUsersGPN.csv # Input File with UserLogonNames
write-host "NTRUsersGPN Count = $($NTRUsers.Count)"
## Connect Azure Subscription with your own method
# Connect-AzAccount -AccessToken
#FileShare Details from which the user profiles needs to removed
$StorageAccountName="<Name>"
$fileShareName="<Share Name" # Only one fileshare at a time, to avoid the deletion of profiles in different shares.
$ResourceGroup = (Get-AzResource -Name $StorageAccountName).ResourceGroupName
$Key = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroup -Name $StorageAccountName
$context=New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $Key[1].Value
$finalResult=@()
$FilesDirectory=Get-AzStorageFile -Context $context -ShareName $fileShareName
write-host "FilesDirectory Count = $($FilesDirectory.Count)"
$i=1
foreach($FilesD in $FilesDirectory)
{
foreach($NTRUser in $NTRUsers)
{
if($FilesD.Name -cmatch $NTRUser.UserGPN)
{
try
{
$Files=$FilesD|Get-AzStorageFile
foreach($File in $Files)
{
Remove-AzStorageFile -Context $context -ShareName $fileShareName -Path $File.ShareFileClient.Path
}
Remove-AzStorageDirectory -Context $context -ShareName $fileShareName -Path $FilesD.Name
$obj=""|select User,Directory,Status
$obj.User=$NTRUser.UserGPN
$obj.Directory=$FileD.Name
$obj.Status="Success"
$finalResult+=$obj
}
catch
{
$obj=""|select User,Directory,Status
$obj.User=$NTRUser.UserGPN
$obj.Directory=$FileD.Name
$obj.Status=$_.Exception.Message
$finalResult+=$obj
}
}
}
$i=$i+1
}
$finalResult | Export-Csv -Path "$path\$($fileShareName)FileSharesDeleteOutput.csv" -Append -Force -NoTypeInformation