本文主要給大家簡單講講用Powershell 腳本如何修改用戶配置文件,相關(guān)專業(yè)術(shù)語大家可以上網(wǎng)查查或者找一些相關(guān)書籍補充一下,這里就不涉獵了,我們就直奔主題吧,希望用Powershell 腳本如何修改用戶配置文件這篇文章可以給大家?guī)硪恍嶋H幫助。
為花山等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及花山網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為網(wǎng)站設計、成都網(wǎng)站制作、花山網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
以其他管理員身份登錄計算機;
確認該用戶abc已經(jīng)退出登錄狀態(tài),可以通過任務管理器或者quser來操作
修改C:\users\abc 的文件名為新的用戶名C:\users\abc1
修改注冊表,這個里面有一堆根據(jù)SID命名的key,需要找到對應的,然后修改對應的profileImagePath
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

創(chuàng)建新的symboLink連接從 c:\users\abc <==> c:\users\abc1。windows下面有自帶的mklink命令可以使用,比如 mklink /D c:\users \abc c:\users\abc1。PS5以后可以用New-item創(chuàng)建,但是早期的版本沒有原生的PS命令,只能間接調(diào)用cmd,或者自己寫一個方法
上面的操作都可以通過PS腳本來實現(xiàn)。
#創(chuàng)建SymLink的方法,這個網(wǎng)上發(fā)現(xiàn)有現(xiàn)成的,我就直接下載了
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跳出循環(huán)
$flag=$true
while($flag){
$oldName=read-host "Please input the old user name"
write-host 'Searching user profile..' -ForegroundColor Cyan
#測試該用戶是否已經(jīng)登錄,這里有個小技巧把quser的字符串結(jié)果轉(zhuǎn)換為對象,具體解釋參考博客
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
#如果已經(jīng)登錄,那么強行退出這個用戶
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];
#根據(jù)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
#調(diào)用上面的方法,創(chuàng)建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
}
}執(zhí)行效果,我直接把這個文件扔到一個遠程電腦的C盤下測試,然后以本地管理員身份登錄,執(zhí)行這個腳本,成功!


用Powershell 腳本如何修改用戶配置文件就先給大家講到這里,對于其它相關(guān)問題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會捕捉一些行業(yè)新聞及專業(yè)知識分享給大家的。
網(wǎng)頁名稱:用Powershell腳本如何修改用戶配置文件
轉(zhuǎn)載注明:http://chinadenli.net/article38/iidjsp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、自適應網(wǎng)站、手機網(wǎng)站建設、ChatGPT、用戶體驗、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)