Hi all I am having some issues, and I am a beginner. I wrote a script that creates a registry value with logging.
It gets stuck on -Type DWORD
.
I know the registry is a virtual drive, and check using Get-PSDrive
.
Can you advise what I am doing wrong? Thanks
This is the error message:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/6/2025 6:59 PM 0 System
Registry key created: HKLM\Software\Microsoft\Windows\CurrentVersion\policies\System
Error: A parameter cannot be found that matches parameter name 'PropertyType'.
Script completed.
Here is the script:
# Define the directory path and log file name
$directoryPath = "C:\setup"
$logFileName = "install_registry_log.txt"
$logFilePath = Join-Path -Path $directoryPath -ChildPath $logFileName
# Check if the directory exists, if not, create it
if (-not (Test-Path -Path $directoryPath)) {
New-Item -Path $directoryPath -ItemType Directory
Write-Host "Directory created at: $directoryPath"
} else {
Write-Host "Directory already exists: $directoryPath"
}
# Create or append to the log file
if (-not (Test-Path -Path $logFilePath)) {
# Create the log file and add a header
New-Item -Path $logFilePath -ItemType File -Force
Add-Content -Path $logFilePath -Value "Log started at: $(Get-Date)"
Write-Host "Log file created: $logFilePath"
} else {
Write-Host "Log file already exists: $logFilePath"
}
# Define the registry key and value to be set
$registryPath = "HKLM\Software\Microsoft\Windows\CurrentVersion\policies\System"
$registryValueName = "DoNotDisplayLastUserName"
# $registryValueData = "1"
# Install registry key settings
try {
# Check if the registry key exists
if (-not (Test-Path -Path $registryPath)) {
# Create the registry key if it doesn't exist
New-Item -Path $registryPath -Force
Add-Content -Path $logFilePath -Value "Registry key created: $registryPath at $(Get-Date)"
Write-Host "Registry key created: $registryPath"
}
# Set the registry value
Set-ItemProperty -Path $registryPath -Name $registryValueName -Value $registryValueData -Type DWORD -Force
Add-Content -Path $logFilePath -Value "Registry value set: $registryPath\$registryValueName = $registryValueData at $(Get-Date)"
Write-Host "Registry value set: $registryPath\$registryValueName = $registryValueData"
} catch {
# Log any errors
Add-Content -Path $logFilePath -Value "Error: $_ at $(Get-Date)"
Write-Host "Error: $_"
}
# Optionally, log the completion of the script
Add-Content -Path $logFilePath -Value "Script completed at: $(Get-Date)"
Write-Host "Script completed."