We are using approx 300+ Windows10 pc in our Network and we have to set sound scheme from "Windows Default" to "No sound" can it possible with Powershell ?
Here i Found
# Set Sound Schemes to no sound
Write-Host "`nSetting Sound Schemes to 'No Sound' .." -foregroundcolor Gray
$Path = "HKCU:\AppEvents\Schemes"
$Keyname = "(Default)"
$SetValue = ".None"
$TestPath = Test-Path $Path
if (-Not($TestPath -eq $True)) {
Write-Host " Creating Folder.. " -foregroundcolor Gray
New-item $path -force
}
if (Get-ItemProperty -path $Path -name $KeyName -EA SilentlyContinue) {
$Keyvalue = (Get-ItemProperty -path $Path).$keyname
if ($KeyValue -eq $setValue) {
Write-Host " The Registry Key Already Exists. " -foregroundcolor green
}
else {
Write-Host " Changing Key Value.. " -foregroundcolor Gray
New-itemProperty -path $Path -Name $keyname -value $SetValue -force # Set 'No Sound' Schemes
Get-ChildItem -Path "HKCU:\AppEvents\Schemes\Apps" | # Apply 'No Sound' Schemes
Get-ChildItem |
Get-ChildItem |
Where-Object { $_.PSChildName -eq ".Current" } |
Set-ItemProperty -Name "(Default)" -Value ""
Write-Host " The Registry Key Value Changed Sucessfully. " -foregroundcolor green
}
}
else {
Write-Host " Creating Registry Key.. " -foregroundcolor Gray
New-itemProperty -path $Path -Name $keyname -value $SetValue -force
Get-ChildItem -Path "HKCU:\AppEvents\Schemes\Apps" |
Get-ChildItem |
Get-ChildItem |
Where-Object { $_.PSChildName -eq ".Current" } |
Set-ItemProperty -Name "(Default)" -Value ""
Write-Host " The Registry Key Created Sucessfully. " -foregroundcolor green
}
Related
I am trying to write a script which takes a source-path as input and copies all files that have been changed since a given date, including their directory structure:
param([string]$source,[string]$datum)
(Get-ChildItem $source -Recurse | Where-Object { $_.LastWriteTime -ge $datum }) | Copy-Item -Destination C:\tmp -Recurse
It works. The problem is, that it copies the files, including their directory structure, BUT it also copies all files from the source directories to the base path of the destination folder.
Where is the error?
First and foremost you should be using robocopy for something like this. It has switches that likely handle your query:
/e Copies subdirectories. Note that this option includes empty directories. For additional information, see Remarks.
/maxage: Specifies the maximum file age (to exclude files older than N days or date).
/l Specifies that files are to be listed only (and not copied, deleted, or time stamped).
So knowing that I would start experimenting with robocopy $source $destination /e /maxage:30 /l which should show all files in the directory tree that were modified in the last 30 days. /maxage might not be the switch you are looking for but its a fair guess.
The issue with your current logic is likely that Get-ChildItem $source -Recurse will be returning folders as well as files. So you could be passing a folder to Copy-Item which is where all the extra files are coming from. You should be able to mitigate that with the -File switch.
Get-ChildItem $source -Recurse -File
Here is an overly complicated solution. It has a lot of error handling with 5 retry attempts. You should be able to apply it toward your needs.
I would look at Line 21 to add your Where-Object and test it.
Example Use: Copy-Files -Source "C:\Path\" -Destination "C:\Path2\"
Function Retry-Command {
$Script:Counter = $Script:Counter + 1
Write-Host "Attempt #" $Script:Counter "out of 5"
if ($Script:Counter -ge 5) {
PAUSE
}
else {
Start-Sleep -Seconds 5 -Verbose
}
}
Function Copy-Files ([string]$Source, [string]$Destination ) {
[System.Collections.ArrayList]$Folder_Content = #()
[string]$Folder_Name = Split-Path $Source -Leaf
[string]$Folder_Path = ($Source -replace [regex]::Escape($Source), ($Destination + $Folder_Name + "\"))
[int]$Script:Counter = 0
while (($Folder_Content.Count -eq "0") -and ($Counter -ne 5)) {
try {
Write-Host ""
Write-Host "------------------------------" -ForegroundColor Cyan
Write-Host "Getting Content of" $Folder_Name -ForegroundColor Yellow
[System.Collections.ArrayList]$Folder_Content = #(Get-ChildItem -Path $Source -Recurse -Force -ErrorVariable Child_Error)
if ((-Not $Child_Error) -and ($Folder_Content.Count -ne 0) ) {
Write-Host "Finished Getting Content of" $Folder_Name -ForegroundColor Green
[int]$Script:Counter = 0
}
else {
Write-Host "Failed to get Content of" $Folder_Name -ForegroundColor Red
Retry-Command
}
}
catch {
Write-Host "Unexpected Error getting Content of" $Folder_Name -ForegroundColor Red
Retry-Command
}
}
if (-Not $Folder_Content.Count -eq 0) {
while ((Test-Path -Path $Folder_Path) -eq $false) {
try {
Write-Host ""
Write-Host "Creating" $Folder_Name "Folder" -ForegroundColor Yellow
Copy-Item -Path $Source -Destination $Folder_Path -Force -ErrorVariable Folder_Error
if (-Not $Folder_Error) {
Write-Host "Finished Creating" $Folder_Name "Folder" -ForegroundColor Green
[int]$Script:Counter = 0
}
else {
Write-Host "Failed to create" $Folder_Name "Folder" -ForegroundColor Red
Retry-Command
}
}
catch {
Write-Host "Unexpected Error Creating" $Folder_Name "Folder" -ForegroundColor Red
Retry-Command
}
}
foreach ($Index in $Folder_Content) {
[string]$Software_File_Name = Split-Path $Index.FullName -Leaf
[string]$Software_File_Path = ($Index.FullName -replace [regex]::Escape($Source), ($Destination + (Split-Path $Source -Leaf) + "\"))
while ((Test-Path -Path $Software_File_Path) -eq $false) {
try {
Write-Host ""
Write-Host "Copying" $Index.FullName -ForegroundColor Yellow
Copy-Item -Path $Index.FullName -Destination $Software_File_Path -Force -ErrorVariable File_Error
if (-Not $File_Error) {
Write-Host "Finished Copying" $Index.FullName -ForegroundColor Green
[int]$Script:Counter = 0
}
else {
Write-Host "Failed to Copy" $Index.FullName -ForegroundColor Red
Retry-Command
}
}
catch {
Write-Host "Unexpected Error Copying" $Index.FullName -ForegroundColor Red
Retry-Command
}
}
}
}
}
So I am trying to copy all the folders and keep the structure that only contain pdf's. I am not sure if there is a way to do this, I have managed to get some of the script to work.
However, when I run it, it copies all the folders even if they don't have PDFs in them. Also I have noticed that my script fails to check the folder and creates in the next directory down. For example:
C:\temp copied once shows C:\temp.
If I was to run the script again it now shows C:\temp\temp
Below is my Code:
$Criteria = *.pdf
$Trial = c:\temp\folders.txt
$Server = \\file
$Path = homedrives\home
$des = $Path
$safe = Get-Content $Trial
$safe | ForEach-Object {
#find drive-delimeter
$first = $_.IndexOf("\\");
if ($first -eq 1) {
#stripe it
$newdes = Join-Path -Path $des -ChildPath #($_.Substring(0,1)+$_.Substring(2))[0]
} else {
$newdes = Join-Path -Path $des -ChildPath $_
}
$err = 0
$fr = 0
$folder = Split-Path -Path $newdes -Parent
#check if folder exists"
$void = Get-Item $folder -ErrorVariable err -ErrorAction SilentlyContinue
if ($err.Count -ne 0) {
#create when it doesn't
$void = New-Item -Path $folder -ItemType Directory -Force
}
#$void = Copy-Item -Path $_ -destination $newdes -Force -Verbose
$void = Copy-Item -Path $_ -destination $newdes -Filter $Criteria -Recurse -Force -ErrorVariable fr -ErrorAction SilentlyContinue
$CR = "`r`n"
$RR = $fr[0].CategoryInfo.TargetName.ToString()
"List of PDF's that Failed To Copy" + $CR + "--------------------------------------------------------------" + $CR + $RR | Out-file -Append $Er
Write-Host $_
}
Write-Host $newdes
ok, so looks like I have cracked it, for future reference, my code actually uses user input to determine locations, it then selects the criteria the user requested and then copies only files with the extensions defined. It copies the folder structure without duplicating and although the filename limitations of windows still exist, there is an error log that is produced that shows the files that have failed to copy. Thanks for all your assistance in this matter.
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null -ErrorAction Stop
$Server = "\\" + [Microsoft.VisualBasic.Interaction]::InputBox("Please choose a Server to search", "Server Choice")
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null -ErrorAction Stop
$Choice = [Microsoft.VisualBasic.Interaction]::InputBox("Please choose a File Path to search", "File Path Choice") + "\"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null -ErrorAction Stop
$Ext = [Microsoft.VisualBasic.Interaction]::InputBox("Please choose a File type i.e. *.PST", "Location Choice")
#$Ext.ToUpper()
$Criteria = "*." + $Ext
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null -ErrorAction Stop
#$FPath = [Microsoft.VisualBasic.Interaction]::InputBox("Please choose a path to copy to", "Location Choice")
$Path = [Microsoft.VisualBasic.Interaction]::InputBox("Please choose a folder to store the data", "Path Choice") + "\"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null -ErrorAction Stop
#$Name = [Microsoft.VisualBasic.Interaction]::InputBox("Please choose a filename", "File Name Choice")
$Search = $Server +"\"+ $Choice
$FileName = $Path
$Info = $Path + "FileCopy.txt"
$Trial = $Path + "Folders.txt"
$Type = $Path + "$Ext's.txt"
$Er= $Path + "Failed Copying.txt"
$Trial2 = $Path + "Folders.txt"
#$Server2 = "\" + $Server
#$File = $Path + $Name
if( -Not (Test-Path -Path $Path ) )
{
New-Item -ItemType directory -Path $Path |out-null
}
Else{
[System.Windows.MessageBox]::Show('The directory already exists','Error','Ok','Error')
-ErrorAction SilentlyContinue
}
$properties = #(
'Directory'
' '
'Name'
' '
#{
Label = 'Size'
Expression = {
if ($_.Length -ge 1GB)
{
'{0:F2} GB' -f ($_.Length / 1GB)
}
elseif ($_.Length -ge 1MB)
{
'{0:F2} MB' -f ($_.Length / 1MB)
}
elseif ($_.Length -ge 1KB)
{
'{0:F2} KB' -f ($_.Length / 1KB)
}
else
{
'{0} bytes' -f $_.Length
}
}
}
$result = Get-ChildItem -Path $Search -Recurse -Include $Criteria -ErrorAction SilentlyContinue
) | Out-Null
$Result
#Get-ChildItem -Path $Search -Recurse -Include *.pst -ErrorAction SilentlyContinue |
$Folders = (get-childitem -Path $Search | where-object { $_.PSIsContainer }).Count
If (Test-Path $Search) {
<#Write-Host
Write-Host "Listing All Found In $Path" -ForegroundColor "Yellow"
Write-Host "=========================================" -ForegroundColor "Yellow"#>
Add-Type -assembly System.Windows.Forms
## -- Create The Progress-Bar
$ObjForm = New-Object System.Windows.Forms.Form
$ObjForm.Text = "Progress-Bar of searched folders"
$ObjForm.Height = 100
$ObjForm.Width = 500
$ObjForm.BackColor = "White"
$ObjForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$ObjForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
## -- Create The Label
$ObjLabel = New-Object System.Windows.Forms.Label
$ObjLabel.Text = "Starting. Please wait ... "
$ObjLabel.Left = 5
$ObjLabel.Top = 10
$ObjLabel.Width = 500 - 20
$ObjLabel.Height = 15
$ObjLabel.Font = "Tahoma"
## -- Add the label to the Form
$ObjForm.Controls.Add($ObjLabel)
$PB = New-Object System.Windows.Forms.ProgressBar
$PB.Name = "PowerShellProgressBar"
$PB.Value = 0
$PB.Style="Continuous"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 500 - 40
$System_Drawing_Size.Height = 20
$PB.Size = $System_Drawing_Size
$PB.Left = 5
$PB.Top = 40
$ObjForm.Controls.Add($PB)
## -- Show the Progress-Bar and Start The PowerShell Script
$ObjForm.Show() | Out-Null
$ObjForm.Focus() | Out-NUll
$ObjLabel.Text = "Starting. Please wait ... "
$ObjForm.Refresh()
Start-Sleep -Seconds 1
Out-Null
## -- Execute The PowerShell Code and Update the Status of the Progress-Bar
$result = Get-ChildItem -Path $Search -Recurse -Include $Criteria -ErrorAction SilentlyContinue
$Counter = 0
ForEach ($Item In $Result) {
## -- Calculate The Percentage Completed
$Counter++
[Int]$Percentage = ($Counter/$Result.Count)*100
$PB.Value = $Percentage
$ObjLabel.Text = "Scanning $Folders Folders For $Criteria in $Search"
#$ObjLabel.Text = "Found $counter $Criteria in $Search"
$ObjForm.Refresh()
Start-Sleep -Milliseconds 150
# -- $Item.Name
#"`t" + $Item.Path
}
$ObjForm.Close()
#Write-Host "`n"
}
Else {
#Write-Host
#Write-Host "`t Cannot Execute The Script." -ForegroundColor "Yellow"
#Write-Host "`t $Search Does Not Exist in the System." -ForegroundColor "Yellow"
#Write-Host
}
Out-Null
Sort-Object -Property DirectoryName |
Format-Table -Property $properties |
out-file $Info
$Search| out-file -Append $Trial
#$Search | Out-File -Append $Trial2
$result | out-file -Append $Info
$result | Select Name | out-file -Append $Type
$CR = "`r`n"
"List of PDF's that Failed To Copy" + $CR + $CR + "--------------------------------------------------------------" | Out-file $Er
$des = $Path
#$PDFs= get-content $Type
$safe = Get-Content $Trial
#$Ten = #($Criteria)
$safe | ForEach-Object{
#find drive-delimeter
$first=$_.IndexOf("\\");
if($first -eq 0){
#stripe it
$newdes=Join-Path -Path $des -ChildPath #($_.Substring(0,1)+$_.Substring(2))[0]
}
$newdes=Join-Path -Path $des -ChildPath $_
$err=0
$fr=0
$folder=Split-Path -Path $newdes -Parent
$folders=Split-Path -Path $newdes -leaf
#$fy = $folder + "\" +$folders
#check if folder exists"
$void=Get-Item $newdes -ErrorVariable err -ErrorAction SilentlyContinue #-verbose
If(!(Test-Path -Path $newdes) ){
$err=1
}
if($err.Count -ne 0){
#create when it doesn't
$void=New-Item -Path $folder -ItemType Directory #-verbose
}
#$void=Copy-Item -Path $_ -destination $newdes -Force -Verbose
$void=Copy-Item -Path $_ -destination $folder -Filter $Criteria -Recurse -Container -Force -ErrorVariable fr -ErrorAction SilentlyContinue -verbose
write-host "------------------------------------------------------- Break -------------------------------------------------------" -ForegroundColor Green
foreach($re in $fr){
$RR = $re[0].CategoryInfo.TargetName.ToString()
$X5 = $re[0].CategoryInfo.Reason.ToString()
$X6 = $re[0].TargetObject.DirectoryName.ToString()
$X6 + $CR + $X5 + "---" + $RR + $CR | Out-file -Append $Er
}
write-host "`n" $_
}
write-host "`n------------------------------------------------------- End of Script -------------------------------------------------------" -ForegroundColor Blue`
I am unable to delete zip folders older than 14 days from a directory. The rest of my script works fine, however the following code block is failing:
It has not thrown any errors, the path has been sanity checked, however, I placed old documents in to see if they are cleared up to no avail. It prints the statement that the folder path exists however does not delete anything from the folder.
if (!(Test-Path $folderpath)) {
New-Item -ItemType Directory -Path $folderpath
Write-Host "$folderpath created"
} else {
Write-Host "$folderpath already exists, will remove old items"
$HowOld = -14
Get-ChildItem $Path -Recurse |
where {$_.LastWriteTime -lt (Get-Date).AddDays($HowOld) -and -not $_.PSIsContainer} |
% {Remove-Item $_.FullName -Force -WhatIf}
}
Here is the full script for completeness:
#Get backup folder drive letter
$letter = [System.IO.DriveInfo]::getdrives() | Where-Object {$_.DriveType -eq 'Fixed' -and $_.Name -notlike '*C*'} | Select-Object -Property Name
[string] $drive = "$letter"
$Drive2 = $null
$Drive2 = $drive.Substring($drive.IndexOf('=')+1)
$BackupDrive = $Drive2.Substring(0, $Drive2.IndexOf('}'))
#Check the programname log path.
$logspath = "C:\ProgramData\programname\programname4.0\Logs"
If (!(test-path $logspath))
{
$IQlogspath = "C:\ProgramData\programname\iQSonar4.0\Logs"
}
Else
{
$IQlogspath = "C:\ProgramData\programname\programname4.0\Logs"
}
#check if backup folder exists, clean if exists and if not create it.
$folderpath = $BackupDrive + "programname_logs"
If(!(test-path $folderpath))
{
New-Item -ItemType Directory -Path $folderpath
write-host "$folderpath created"
}
Else
{
write-host "$folderpath already exists, will remove old items"
$HowOld = -14
get-childitem $Path -recurse | where {$_.lastwritetime -lt (get-date).adddays($HowOld) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -whatif}
}
#check if todays folder exists, if not create it
$todaysdate = (Get-Date).ToString('dd-MM-yyyy')
$todaysbackup = $folderpath + "\Logs_Backup_" + $todaysdate
$todaysbackupzip = $todaysbackup + ".zip"
If(!(test-path $todaysbackup))
{
New-Item -ItemType Directory -Path $todaysbackup
write-host "$todaysbackup created, now moving items"
#select and move files to backup folder
get-childitem -Path $IQlogspath |
where-object {$_.LastWriteTime -lt (get-date).AddDays(-7) -and $_.Name -notmatch "Service"} |
move-item -destination $todaysbackup
Start-Sleep -s 10
write-host "checking for zip"
If(Test-path $todaysbackupzip) {
write-host "$todaysbackupzip already exists, changing name"
$todaysbackupzip = $null
$todaysbackupzip = $folderpath + "\Logs_Backup_" + $todaysdate + ".re-run" + ".zip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($todaysbackup, $todaysbackupzip)
write-host "$todaysbackupzip created"
Remove-Item -Recurse -Force $todaysbackup
write-host "$todaysbackup removed"
exit
}
write-host "creating zip folder, $todaysbackupzip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($todaysbackup, $todaysbackupzip)
write-host "$todaysbackupzip created"
Remove-Item -Recurse -Force $todaysbackup
write-host "$todaysbackup removed"
exit
}
Else
{
write-host "$todaysbackup already exists, attempting to zip"
#check if zip backup folder already exists
If(Test-path $todaysbackupzip) {
write-host "$todaysbackupzip already exists, changing name"
$todaysbackupzip = $null
$todaysbackupzip = $folderpath + "\Logs_Backup_" + $todaysdate + ".re-run" + ".zip"
[io.compression.zipfile]::CreateFromDirectory($todaysbackup, $todaysbackupzip)
write-host "$todaysbackupzip created"
Remove-Item -Recurse -Force $todaysbackup
write-host "$todaysbackup removed"
exit
}
Else
{
Start-Sleep -s 10
write-host "creating zip folder, $todaysbackupzip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($todaysbackup, $todaysbackupzip)
write-host "$todaysbackupzip created"
Remove-Item -Recurse -Force $todaysbackup
write-host "$todaysbackup removed"
exit
}
}
#Get OS disk label
$OSDisk = Get-WmiObject Win32_OperatingSystem | Select-Object WindowsDirectory
[string] $drive = "$OSDisk"
$Drive2 = $null
$Drive2 = $drive.Substring($drive.IndexOf('=')+1)
$OSDrive = $Drive2.Substring(0, $Drive2.IndexOf('\'))
$OSDrive2 = "'$OSDrive'"
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID=$OSDrive2"| Select-Object FreeSpace
[INT] $FreeSpace = $disk.FreeSpace /1GB
If ($FreeSpace -gt 5) {
write-host "no additional backup requirement, exiting script"
exit
}
Else {
write-host "Running one off clear"
$OneOff = $IQlogspath + "\Logs_One_Off_" + $todaysdate
$OneOffZip = $OneOff + ".zip"
If(!(test-path $OneOff))
{
New-Item -ItemType Directory -Path $OneOff
write-host "$OneOff created"
}
Else {
write-host "folder already exists"
}
#select and move files to backup folder
write-host "moving logs to $OneOff"
get-childitem -Path $IQlogspath |
where-object {$_.Name -notmatch "Service"} |
move-item -destination $OneOff
Start-Sleep -s 10
write-host "creating zip folder, $todaysbackupzip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($OneOff, $OneOffZip)
write-host "$todaysbackupzip created"
Remove-Item -Recurse -Force $todaysbackup
write-host "$todaysbackup removed"
exit
}
I'm writing a script that will modify some registry settings on Windows machines.
I'm using an If to determine if the key/property exist before writing them. It should fail and write an error if it does. This chunk is working:
$currentPath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext'
$pathTest = Test-Path $currentPath
Write-Host -ForegroundColor Yellow 'Testing for: ' $currentPath
If($pathTest -ne 'True')
{
New-Item -Path $currentPath -Force | Out-Null
Write-Host -ForegroundColor Green 'Created Path: '$currentPath
}
Else{
Write-Host -ForegroundColor Red $currentPath ' already exists.'
}
Write-Host -ForegroundColor Yellow 'Disabling IE Add-On Performance Notifications (DisableAddonLoadTimePerformanceNotifications):'
If((Get-ItemProperty $currentPath -Name 'DisableAddonLoadTimePerformanceNotifications' -ea 0).'DisableAddonLoadTimePerformanceNotifications')
{
Write-Host -ForegroundColor Red 'DisableAddonLoadTimePerformanceNotifications already exists'
}
Else {
New-ItemProperty -Path $currentPath -Name DisableAddonLoadTimePerformanceNotifications -PropertyType DWord -Value '1' | Out-Null
Write-Host -ForegroundColor Green 'DisableAddonLoadTimePerformanceNotifications created and set to 1'
}
It gives me this output if the key/property exists:
Testing for: HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext
HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext already exists.
Disabling IE Add-On Performance Notifications (DisableAddonLoadTimePerformanceNotifications):
DisableAddonLoadTimePerformanceNotifications already exists
However, this chunk does not work:
$currentPath = 'HKLM:\Software\Policies\Microsoft\Internet Explorer\Main'
$pathTest = Test-Path $currentPath
Write-Host -ForegroundColor Yellow 'Testing for: ' $currentPath
If($pathTest -ne 'True')
{
New-Item -Path $currentPath -Force | Out-Null
Write-Host -ForegroundColor Green 'Created Path: '$currentPath
}
Else{
Write-Host -ForegroundColor Red $currentPath ' already exists.'
}
If((Get-ItemProperty $currentPath -Name 'EnableAutoUpgrade' -ea 0).'EnableAutoUpgrade')
{
Write-Host -ForegroundColor Red 'EnableAutoUpgrade already exists'
}
Else{
New-ItemProperty -Path $currentPath -Name 'EnableAutoUpgrade' -PropertyType DWord -Value '0' | Out-Null
Write-Host -ForegroundColor Green 'EnableAutoUpgrade created and set to 0'
}
Output if the key/property exists:
Testing for: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main already exists.
New-ItemProperty : The property already exists.
At C:\ps\setup\2.ps1:42 char:10
+ New-ItemProperty -Path $currentPath -Name 'EnableAutoUpgrade' -Property ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (HKEY_LOCAL_MACH...t Explorer\Main:String) [New-ItemProperty], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand
EnableAutoUpgrade created and set to 0
Output of the key/property does not exist:
Testing for: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main
Created Path: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main
Disabling IE Auto Upgrade (EnableAutoUpgrade):
EnableAutoUpgrade created and set to 0
So, it'll create the property no problem, but can't see if it already exists.It looks to me like the If is failing on this one, but I can't figure out why. It's the same as one above it. In the full script I run a total of 14 registry entries. The ones above EnableAutoUpgrade all work. The ones below EnableAutoUpgrade do not and fail with the same error.
This is happening on multiple machines all running Win8.1 and PowerShell v4.
This is a real head scratcher. Any help would be appreciated.
Try this:
$currentPath = "HKLM:\Software\Policies\Microsoft\Internet Explorer\Main"
Write-Host -ForegroundColor Yellow "Testing for: " $currentPath
If(Test-Path "HKLM:\Software\Policies\Microsoft\Internet Explorer\Main")
{
Write-Host $currentPath " already exists." -ForegroundColor 'Red'
}
Else{
New-Item -Path $currentPath | Out-Null
Write-Host "Created Path: "$currentPath -ForegroundColor 'Green'
}
In 'HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' I have some paths set to an old server. e.g.:
'My Pictures' is set to '\\DeadServer\RedirectedFolders\%UserName%\My Documents\My Pictures'
I'd like to replace "\\DeadServer\RedirectedFolders" with "C:\Users"
How can this be done in powershell?
I got as far as trying
Get-ItemProperty -path "Microsoft.PowerShell.Core\Registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" | ? {$_.PSObject.Properties -like "*DeadServer*"}
But I think I'm getting confused with how the entry I want to change is a 'Property', and not an 'Item', and I don't know how to iterate through properties like I'd do Items.
Before you ask, I've already made this change with Group Policy, but it's not taking. Users are getting a message
The Recycle Bin on \DeadServer\RedirectedFolders\%UserName%\My Documents\My Pictures` is corrupted. Do you want to empty the Recycle Bin for this drive?
upon login which is keeping Folder Redirection from applying.
This is my attempt to force the change back to local storage manually.
I figured it out. Took me a long time, but I wrote a rather inelegant script:
Get-Item -ErrorAction SilentlyContinue -path "Microsoft.PowerShell.Core\Registry::HKEY_USERS\*\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" |
foreach {
Get-ItemProperty -Path "Microsoft.PowerShell.Core\Registry::$_" |
foreach {
$CurrentUserShellFoldersPath = $_.PSPath
$SID = $CurrentUserShellFoldersPath.Split('\')[2]
$_.PSObject.Properties |
foreach {
if ($_.Value -like "*DeadServer*") {
write-host "Path:`t`t"$CurrentUserShellFoldersPath
write-host "SID:`t`t"$SID
write-host "Name:`t`t"$_.Name
write-host "Old Value:`t"$_.Value
$newValue = $_.Value
$newValue = $newValue -replace '\\\\DeadServer\\RedirectedFolders', "C:\Users"
$newValue = $newValue -replace "My Documents\\", ""
$newValue = $newValue -replace "My ", ""
Write-Host "New Value:`t"$newValue
Set-ItemProperty -Path $CurrentUserShellFoldersPath -Name $_.Name -Value $newValue
Write-host "================================================================"
}
}
}
}
I'd love to learn of a faster or more elegant way to do this if any of you have one.
Here's an easy to use registry replace function, which can search a path recursively.
# Replace all registry key values and/or registry key names under a given path.
# Example Usage:
# RegistryValue-Replace "ExistingValue" "NewValue" 'HKEY_CURRENT_USER\Software\100000_DummyData'
# RegistryValue-Replace "ExistingValue" "NewValue" 'HKEY_USERS\*\Software\100000_DummyData' -ReplaceKeyNames $true -CaseSensitive $true
# RegistryValue-Replace 'C:\\Program Files\\Microsoft SQL Server' 'E:\Program Files\Microsoft SQL Server' 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server*' -LoggingOn $true
function RegistryValue-Replace (
[string]$OldValue = $(throw “OldValue (the current value) required.”),
[string]$NewValue = $(throw “NewValue (the replacement value) required.”),
[string]$RegkPath = $(throw “RegkPath (The full registry key path) required.”),
[bool] $CaseSensitive = $false, # If true, search and replace is case sensitive
[bool] $WholeWord = $false, # If true, searches for whole word within the value.
[bool] $ExactMatch = $false, # If true, the entire value must match OldValue, and partial replacements are NOT performed
[bool] $ReplaceKeyNames = $false, # If true, replaces registry key names
[bool] $ReplaceValues = $true,
[bool] $LoggingOn = $false )
{
$PowershellRegPrefix = 'Microsoft.PowerShell.Core\Registry::'
$MatchFor = if ($WholeWord -eq $true) {".*\b$OldValue\b.*"} else { ".*$OldValue.*" }
if ($RegkPath -NotLike "$PowershellRegPrefix*") { $RegkPath = $PowershellRegPrefix + $RegkPath }
#(Get-Item -ErrorAction SilentlyContinue -path $RegkPath) +
#(Get-ChildItem -Recurse $RegkPath -ErrorAction SilentlyContinue) |
foreach {
Get-ItemProperty -Path "$PowershellRegPrefix$_" |
foreach {
$CurrentShellFoldersPath = $_.PSPath
$SID = $CurrentShellFoldersPath.Split('\')[2]
$_.PSObject.Properties |
foreach {
if ($_.Name -cne "PSChildName" -and (($ExactMatch -eq $true -and $_.Value -clike $OldValue) -or ($ExactMatch -eq $false -and
(($CaseSensitive -eq $false -and $_.Value -match $MatchFor) -or ($CaseSensitive -eq $true -and $_.Value -cmatch $MatchFor))))) {
$Original = $_.Value
$Create_NewValue = $_.Value
$SubKeyName = $_.Name
if ($CaseSensitive -eq $true){ $Create_NewValue = $Create_NewValue -creplace $OldValue, $NewValue }
else { $Create_NewValue = $Create_NewValue -replace $OldValue, $NewValue }
if ($_.Name -eq "PSPath" -and $_.Value -eq $CurrentShellFoldersPath) {
if ($ReplaceKeyNames -eq $true) {
Move-Item -Path $CurrentShellFoldersPath -Destination $Create_NewValue
if ($LoggingOn -eq $true){ Write-host "Renamed registry key '$CurrentShellFoldersPath' to '$Create_NewValue'" }
} else {
if ($LoggingOn -eq $true){ Write-host "....Skipping renaming key '$CurrentShellFoldersPath->$SubKeyName' due to input option!!!" } }
} else {
if ($ReplaceValues -eq $true) {
Set-ItemProperty -Path $CurrentShellFoldersPath -Name $_.Name -Value $Create_NewValue
if ($LoggingOn -eq $true){ Write-host "Renamed '$Original' to '$Create_NewValue' for registry key '$CurrentShellFoldersPath->$SubKeyName'" }
} else {
if ($LoggingOn -eq $true){ Write-host "....Skipping renaming value '$CurrentShellFoldersPath->$SubKeyName' due to input option!!!" } }
}
}
}
}
}
}
Not really happy with this so I will be happy and sad if someone puts this to shame. It's been mostly tested as far as verifying that it is locating the correct keys.
If(!(Test-Path HKU:)){New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS}
$registrySearchPath = "HKU:\*\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
$pathToReplace = [regex]::Escape("C:\Users")
$newPath = '%USERPROFILE%'
Get-Item -path $registrySearchPath -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Name |
Where-Object{$_ -match "^HKEY_USERS\\S-1-5-21"} |
ForEach-Object{
$key = $_ -replace "^HKEY_USERS","HKU:"
(Get-ItemProperty $key).psobject.Properties | Where-Object{$_.Value -match $pathToReplace} |
Select-Object Name,Value | ForEach-Object{
Set-ItemProperty -Path $key -Name $_.Name -Value ($_.Value -replace $pathToReplace,$newPath) -WhatIf
}
}
Use a Psdrive to map HKU since its not a default drive in PowerShell. Get all keys back that have at least a path to "\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders". Omit the default keys and any other localesque accounts by only looking for the ones with "S-1-5-21" as part of the key. Then for each of those that is located find every registry value with data that matchs the path you are looking for.
Set-ItemProperty -Path $key -Name $_.Name -Value ($_.Value -replace $pathToReplace,$newPath) -WhatIf
Drawing a little more attention on the last part here. With all the values that matched we replace the data with a simple -replace. I have a -WhatIf on there to be sure you test in case something bad happens. I would suggest commenting out that line and outputing just $_.Value -replace $pathToReplace,$newPath to verify that it is doing what you expect it to.
Make sure that you change the values for $pathToReplace and $newPath then Test twice, execute once.
The following is an example I use for after I rename the path of a profile for a user account:
function get-itemproperty2 {
# get-childitem skips top level key, use get-item for that
# set-alias gp2 get-itemproperty2
param([parameter(ValueFromPipeline)]$key)
process {
$key.getvaluenames() | foreach-object {
$value = $_
[pscustomobject] #{
Path = $Key -replace 'HKEY_CURRENT_USER',
'HKCU:' -replace 'HKEY_LOCAL_MACHINE','HKLM:'
Name = $Value
Value = $Key.GetValue($Value)
Type = $Key.GetValueKind($Value)
}
}
}
}
ls -r hkcu: | get-itemproperty2 | where Value -match "(.*)C:\\Users\\Admin(.*)" | ForEach-Object {
$newkey = $_.Value -replace '(.*)C:\\Users\\Admin(.*)', '$1C:\Users\dennisg$2';
if ($_.Name -eq '')
{
set-itemproperty -Path $_.Path -Name '(Default)' -Value $newkey -Type $_.Type ;
$outInfo = '****' + $_.Path + " | " + $_.Name + " | " + $newkey;
}
else
{
set-itemproperty -Path $_.Path -Name $_.Name -Value $newkey -Type $_.Type ;
$outInfo = $_.Path + " | " + $_.Name + " | " + $newkey;
}
echo $outInfo;
}
Function is from this SO question Use PowerShell to search for string in registry keys and values