Pwershell Script

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

dawber39Database Analyst / Application DeveloperAsked:
Who is Participating?
 
marek1712Commented:
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
0
 
marek1712Commented:
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

0
 
dawber39Database Analyst / Application DeveloperAuthor Commented:
Thank you for your explanation - I really appreciate it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.