We help IT Professionals succeed at work.

Pwershell Script

dawber39
dawber39 asked
on
I am new to Powershell - and I am trying to create a script that will check to see if a registry key exists, if it does - change the value, if it does not exist - then create it and set the value. What I have is below - and it does not work (IWhereas I am new,  am not surprised)
$key = Test-Path HKCU:\Software\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
If $key=False
New-Item –path HKCU:\Software\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters –name AutoShareServer
else
New-Itemproperty -path " HKCU:\Software\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\AutoShareServer  -value  0

Open in new window

Comment
Watch Question

Slight tweaking required:
$key = Test-Path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\AutoShareServer
If ($key -eq $false)
{
    New-Itemproperty -path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\ -PropertyType DWORD -Name AutoShareServer -Value 0
}
else
{
    New-Itemproperty -path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\ -PropertyType DWORD -Name AutoShareServer -Value 0
}

Open in new window

- there's no "=" in PoSh. There is "-eq"
- everything has to be enclosed in brackets or parentheses
- you have to specify WHAT type of value you want to create
Ehh, should be:
$key = Test-Path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\AutoShareServer
If ($key.Exists -eq $false)
{
    New-Itemproperty -path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\ -PropertyType DWORD -Name AutoShareServer -Value 0
}
else
{
    Set-Itemproperty -path HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\ -Name AutoShareServer -Value 0
}

Open in new window

dawber39Database Analyst / Application Developer

Author

Commented:
Thank you for your explanation - I really appreciate it