# Define the path to the CSV file
$csvFile = "C:\Users\Asennus\Desktop\Company_Personnel_Data_With_Umlauts.csv" # Replace with the actual path to your CSV file
# Import user data from the CSV file (specify semicolon as the delimiter)
$users = Import-Csv -Path $csvFile -Delimiter ';'
# Keep track of duplicates, created groups, and processed usernames
$duplicateUsers = @()
$createdGroups = @()
$processedUsers = @()
# Ensure the "Käyttäjät" group exists
if (-not (Get-LocalGroup -Name "Käyttäjät" -ErrorAction SilentlyContinue)) {
Write-Host "Creating group: Käyttäjät"
New-LocalGroup -Name "Käyttäjät" -Description "Default user group"
$createdGroups += "Käyttäjät"
}
# Check and create groups from the CSV if necessary
foreach ($group in ($users.GroupName | Sort-Object -Unique)) {
if (-not (Get-LocalGroup -Name $group -ErrorAction SilentlyContinue)) {
Write-Host "Creating group: $group"
New-LocalGroup -Name $group -Description "Group created via script"
$createdGroups += $group
}
}
# Loop through each user in the CSV file
foreach ($user in $users) {
# Check if the username has already been processed
if ($processedUsers -contains $user.UserName) {
$duplicateUsers += $user.UserName
continue
}
# Mark the username as processed
$processedUsers += $user.UserName
# Check if the user already exists
$existingUser = Get-LocalUser -Name $user.UserName -ErrorAction SilentlyContinue
if ($existingUser) {
# If the user exists, log them as duplicate and skip creation
$duplicateUsers += $user.UserName
} else {
# Create the user
New-LocalUser -Name $user.UserName -Password (ConvertTo-SecureString $user.Password -AsPlainText -Force) -FullName $user.UserName -Description "User created via script"
}
# Add user to the "Käyttäjät" group if not already a member
if (-not (Get-LocalGroupMember -Group "Käyttäjät" -Member $user.UserName -ErrorAction SilentlyContinue)) {
Add-LocalGroupMember -Group "Käyttäjät" -Member $user.UserName -ErrorAction SilentlyContinue
}
# Remove the user from all other non-"Käyttäjät" groups
$existingGroups = Get-LocalGroup | Where-Object {
(Get-LocalGroupMember -Group $_.Name -ErrorAction SilentlyContinue | Where-Object { $_.Name -eq $user.UserName }) -and
($_.Name -ne "Käyttäjät")
}
foreach ($existingGroup in $existingGroups) {
Remove-LocalGroupMember -Group $existingGroup.Name -Member $user.UserName -ErrorAction SilentlyContinue
}
# Add user to the specified group from the CSV
Add-LocalGroupMember -Group $user.GroupName -Member $user.UserName -ErrorAction SilentlyContinue
}
# Print summary
Write-Host "`n--- Script Summary ---"
if ($createdGroups.Count -gt 0) {
Write-Host "The following groups were created:" -ForegroundColor Green
$createdGroups | ForEach-Object { Write-Host "- $_" }
} else {
Write-Host "No new groups were created." -ForegroundColor Yellow
}
if ($duplicateUsers.Count -gt 0) {
Write-Host "`nThe following duplicate usernames in the CSV were skipped (manual handling required):" -ForegroundColor Yellow
$duplicateUsers | ForEach-Object { Write-Host "- $_" }
} else {
Write-Host "`nNo duplicate usernames were found in the CSV." -ForegroundColor Green
}
Write-Host "`nAll assignments completed successfully!" -ForegroundColor Green
删除所有通过脚本创建的用户和组.ps1
# Remove Users with Description "User created via script"
Write-Host "Searching for users with description 'User created via script'..." -ForegroundColor Yellow
$localUsers = Get-LocalUser
$usersToRemove = $localUsers | Where-Object { $_.Description -eq "User created via script" }
if ($usersToRemove.Count -gt 0) {
Write-Host "The following users will be removed:" -ForegroundColor Yellow
$usersToRemove | ForEach-Object { Write-Host "- $($_.Name)" }
# Confirm deletion
$userConfirmation = Read-Host "Type 'YES' to confirm deletion of these users"
if ($userConfirmation -eq "YES") {
$usersToRemove | ForEach-Object {
Write-Host "Removing user: $($_.Name)" -ForegroundColor Red
Remove-LocalUser -Name $_.Name -ErrorAction SilentlyContinue
}
Write-Host "All users with the description 'User created via script' have been removed." -ForegroundColor Green
} else {
Write-Host "Operation cancelled. No users were removed." -ForegroundColor Cyan
}
} else {
Write-Host "No users found with the description 'User created via script'." -ForegroundColor Green
}
# Remove Groups with Description "Group created via script"
Write-Host "`nSearching for groups with description 'Group created via script'..." -ForegroundColor Yellow
$localGroups = Get-LocalGroup
$groupsToRemove = $localGroups | Where-Object { $_.Description -eq "Group created via script" }
if ($groupsToRemove.Count -gt 0) {
Write-Host "The following groups will be removed:" -ForegroundColor Yellow
$groupsToRemove | ForEach-Object { Write-Host "- $($_.Name)" }
# Confirm deletion
$groupConfirmation = Read-Host "Type 'YES' to confirm deletion of these groups"
if ($groupConfirmation -eq "YES") {
$groupsToRemove | ForEach-Object {
Write-Host "Removing group: $($_.Name)" -ForegroundColor Red
Remove-LocalGroup -Name $_.Name -ErrorAction SilentlyContinue
}
Write-Host "All groups with the description 'Group created via script' have been removed." -ForegroundColor Green
} else {
Write-Host "Operation cancelled. No groups were removed." -ForegroundColor Cyan
}
} else {
Write-Host "No groups found with the description 'Group created via script'." -ForegroundColor Green
}
默认安装的 powershell v5.1 和命令在技术上不存在umlouts 问题,因此请检查您的输入是否正确编码:
Powershell (5.1) 的默认安装版本默认假定脚本和文本文件采用带BOM 的 UTF-8编码,因此如果文件采用纯 UTF-8(无 BOM),您可能会遇到问题。例如:
对于脚本文件本身,您可以像这样重新编码文件,它应该可以正常运行:
在这种情况下,Powershell v6 及更新版本默认使用纯 UTF-8,但请注意,运行带有 BOM 的文件时可能会遇到反向问题。
在此处查看更多详细信息:about_Character_Encoding
花了一段时间来真正测试这一点。
包括芬兰语Windows 11的图像和脚本,因为我们在这里使用变音符号或“Ääkköset”。(尽管我讨厌非英语软件或与计算机相关的惯例,但这不是重点)
最重要的是:从 Microsoft Store下载最新的 PowerShell。不要使用 Windows 11 附带的 Windows PowerShell,它已经过时了,并且不适用于变音符号(我试过了,不行,变音符号会变得混乱,就像 Käyttäjät 变成 Käyttäjät,很烦人)。
这里有一些入门材料。PS1 脚本中重要的是“Käyttäjät”一词。在芬兰语中,它表示用户或普通用户。我比较确定,如果您将其替换为 Windows 11 本地化版本中使用的术语,那么您应该就可以开始了。请不要通过缩短或删除脚本来编辑脚本 - 如果我再次需要它们,我会回来查看它们。尽管这条消息已经很长了。
公司人员数据带变音符号.csv
(它们是自动生成的最受欢迎的芬兰名字,与任何事物都无关。)
添加所有用户和组.ps1
删除所有通过脚本创建的用户和组.ps1