本文主要給大家簡單講講用Powershell 腳本如何修改用戶配置文件,相關專業術語大家可以上網查查或者找一些相關書籍補充一下,這里就不涉獵了,我們就直奔主題吧,希望用Powershell 腳本如何修改用戶配置文件這篇文章可以給大家帶來一些實際幫助。
以其他管理員身份登錄計算機;
確認該用戶abc已經退出登錄狀態,可以通過任務管理器或者quser來操作
修改C:\users\abc 的文件名為新的用戶名C:\users\abc1
修改注冊表,這個里面有一堆根據SID命名的key,需要找到對應的,然后修改對應的profileImagePath
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

創建新的symboLink連接從 c:\users\abc <==> c:\users\abc1。windows下面有自帶的mklink命令可以使用,比如 mklink /D c:\users \abc c:\users\abc1。PS5以后可以用New-item創建,但是早期的版本沒有原生的PS命令,只能間接調用cmd,或者自己寫一個方法
上面的操作都可以通過PS腳本來實現。
#創建SymLink的方法,這個網上發現有現成的,我就直接下載了
function New-Symlink {
<#
.SYNOPSIS
Creates a symbolic link.
#>
param (
[Parameter(Position=0, Mandatory=$true)]
[string] $Link,
[Parameter(Position=1, Mandatory=$true)]
[string] $Target
)
Invoke-MKLINK -Link $Link -Target $Target -Symlink
}
function New-Hardlink {
<#
.SYNOPSIS
Creates a hard link.
#>
param (
[Parameter(Position=0, Mandatory=$true)]
[string] $Link,
[Parameter(Position=1, Mandatory=$true)]
[string] $Target
)
Invoke-MKLINK -Link $Link -Target $Target -HardLink
}
function New-Junction {
<#
.SYNOPSIS
Creates a directory junction.
#>
param (
[Parameter(Position=0, Mandatory=$true)]
[string] $Link,
[Parameter(Position=1, Mandatory=$true)]
[string] $Target
)
Invoke-MKLINK -Link $Link -Target $Target -Junction
}
function Invoke-MKLINK {
<#
.SYNOPSIS
Creates a symbolic link, hard link, or directory junction.
#>
[CmdletBinding(DefaultParameterSetName = "Symlink")]
param (
[Parameter(Position=0, Mandatory=$true)]
[string] $Link,
[Parameter(Position=1, Mandatory=$true)]
[string] $Target,
[Parameter(ParameterSetName = "Symlink")]
[switch] $Symlink = $true,
[Parameter(ParameterSetName = "HardLink")]
[switch] $HardLink,
[Parameter(ParameterSetName = "Junction")]
[switch] $Junction
)
# Ensure target exists.
if (-not(Test-Path $Target)) {
throw "Target does not exist.`nTarget: $Target"
}
# Ensure link does not exist.
if (Test-Path $Link) {
throw "A file or directory already exists at the link path.`nLink: $Link"
}
$isDirectory = (Get-Item $Target).PSIsContainer
$mklinkArg = ""
if ($Symlink -and $isDirectory) {
$mkLinkArg = "/D"
}
if ($Junction) {
# Ensure we are linking a directory. (Junctions don't work for files.)
if (-not($isDirectory)) {
throw "The target is a file. Junctions cannot be created for files.`nTarget: $Target"
}
$mklinkArg = "/J"
}
if ($HardLink) {
# Ensure we are linking a file. (Hard links don't work for directories.)
if ($isDirectory) {
throw "The target is a directory. Hard links cannot be created for directories.`nTarget: $Target"
}
$mkLinkArg = "/H"
}
# Capture the MKLINK output so we can return it properly.
# Includes a redirect of STDERR to STDOUT so we can capture it as well.
$output = cmd /c mklink $mkLinkArg `"$Link`" `"$Target`" 2>&1
if ($lastExitCode -ne 0) {
throw "MKLINK failed. Exit code: $lastExitCode`n$output"
}
else {
Write-Output $output
}
}
#定義一個Flag跳出循環
$flag=$true
while($flag){
$oldName=read-host "Please input the old user name"
write-host 'Searching user profile..' -ForegroundColor Cyan
#測試該用戶是否已經登錄,這里有個小技巧把quser的字符串結果轉換為對象,具體解釋參考博客
http://beanxyz.blog.51cto.com/5570417/1906162
if (Test-Path "c:\users\$oldName"){
write-host "User Profile c:\users\$oldName found." -ForegroundColor Cyan
#Check if the user is currently logged In
$quser = (quser) -replace '\s{2,17}', ',' | ConvertFrom-Csv
$sessionId = $quser | Where-Object { $_.Username -eq $newName } | select -ExpandProperty id
#如果已經登錄,那么強行退出這個用戶
foreach($id in $sessionId){
if($id -ne $null){
write-host "Detected User $newName still login" -ForegroundColor red
Write-Host "Force logoff the user" -ForegroundColor red
logoff $id
}
}
$newName=read-host "Please input the new name"
$oldpath="c:\users\$oldName"
$newpath="c:\users\$newName"
#重命名文件夾
rename-item $oldpath $newpath -Confirm -ErrorAction Stop
write-host "Searching Registry Information " -ForegroundColor Cyan
#查詢對應的注冊表Key
Get-ChildItem "hklm:\software\microsoft\windows nt\currentversion\profilelist" | foreach{
#Get the username from SID
$sid=$_.Name.Split('\')[-1];
#根據SID來匹配用戶,如果用戶匹配成功,那么修改對應的ProfileList
try{
$objSID = New-Object System.Security.Principal.SecurityIdentifier ($sid)
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$username=$objUser.Value
}
catch{}
#change registry keys
if(($username -eq "omnicom\$oldName") -or ($username -eq "omnicom\$newName")){
write-host "Found Registry Information of user profile $newName" -ForegroundColor Cyan
$keys=Get-ItemProperty "hklm:\software\microsoft\windows nt\currentversion\profilelist\$sid"
$keys.ProfileImagePath=$newpath
write-host "Registry key profile list is changed to $newpath" -ForegroundColor Cyan
#調用上面的方法,創建Symbolink
#Create new symbolink
#New-Item -Path $oldpath -ItemType Junction -Value $newpath
New-Symlink -Link $oldpath -Target $newpath
break;
}
else{
write-host "$username Name not match...skip" -ForegroundColor Yellow
}
}
$flag=$false
}
else {
write-host "Profile is not found. Please try again" -ForegroundColor red
}
}執行效果,我直接把這個文件扔到一個遠程電腦的C盤下測試,然后以本地管理員身份登錄,執行這個腳本,成功!


用Powershell 腳本如何修改用戶配置文件就先給大家講到這里,對于其它相關問題大家想要了解的可以持續關注我們的行業資訊。我們的板塊內容每天都會捕捉一些行業新聞及專業知識分享給大家的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。