- <#
- .SYNOPSIS
- This script displays the WMI objects associated with a folder
- .DESCRIPTION
- This script uses the Associators Of query
- .NOTES
- File Name : get-folderobjects.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Get-FolderObjects "C:\Foo"
- what's in C:\foo
- 0 Volume objects
- 0 Logical Disk objects
- 10 Directory objects
- 171 File objects
- 0 Pagefile objects
- 1 Security Settings
- 0 Share objects
- 0 other objects
- .EXAMPLE
- Get-FolderObjects "C:\"
- what's in C:\
- 1 Volume objects
- 1 Logical Disk objects
- 18 Directory objects
- 8 File objects
- 1 Pagefile objects
- 1 Security Settings
- 1 Share objects
- 0 other objects
- #>
- # Function to get folder WMI object associations
- # Define function
- Function Get-FolderObjects {
- Param (
- $Folder = "c:\"
- )
- # Set query then get associations
- $query = "ASSOCIATORS OF {Win32_Directory.Name='$folder'}"
- $objs = Gwmi -q $query
- #
- # Now group them
- # Create empty arrays
- $directory = @()
- $datafile = @()
- $pagefile = @()
- $volume = @()
- $filesec = @()
- $share = @()
- $Logdisk = @()
- $unknown = @()
- # Now fill the arrays
- foreach ($obj in $objs) {
- switch ($Obj.__class) {
- "Win32_Directory" {$directory += $obj; break}
- "Cim_DataFile" {$datafile += $obj; break}
- "Win32_PageFile" {$pagefile += $obj; break}
- "Win32_Volume" {$volume += $obj; break}
- "Win32_LogicalDisk" {$logdisk += $obj; break}
- "Win32_LogicalFileSecuritySetting" {$filesec += $obj; break}
- "Win32_share" {$share += $obj; break}
- default {$unknown += $obj; break}
- }
- }
- # Display the output
- " what's in $folder"
- "{0} Volume objects" -f $volume.count
- "{0} Logical Disk objects" -f $logdisk.count
- "{0} Directory objects" -f $directory.count
- "{0} File objects" -f $datafile.count
- "{0} Pagefile objects" -f $pagefile.count
- "{0} Security Settings" -f $filesec.count
- "{0} Share objects" -f $share.count
- "{0} other objects" -f $unknown.count
- }
- # Here call function as an example
- Get-FolderObjects "C:\"
- "***"
- Get-FolderObjects "C:\foo"
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Friday, 30 September 2011
Get-FolderObjects.ps1
Sunday, 25 September 2011
Set-ComputerName.ps1
- <#
- .SYNOPSIS
- This script renames a computer using WMI.
- .DESCRIPTION
- This script uses the Rename method from
- the Win32_OperatingSystem WMI class.
- This is sample 5 from http://msdn.microsoft.com/en-us/library/aa394586
- .NOTES
- File Name : Set-ComputerName.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/aa394586%28VS.85%29.aspx
- .EXAMPLE
- Left as an exercise for the Reader
- #>
- # This script takes two parameters, The computer to
- # rename and then the newname for that computer
- param (
- [$String] $NewName = 'NewName',
- [$string] $Comp = "."
- }
- # Get computer object
- $Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp
- #Rename the Computer
- $Return = $Computer.Rename($NewName)
- if ($return.ReturnValue -eq 0) {
- "Computer name is now: $NewName"
- " but you need to reboot first"
- } else {
- " RenameFailed, return code: {0}" -f $return.ReturnValue
- }
Tuesday, 13 September 2011
Remove-WmiRegistryKey.ps1
- <#
- .SYNOPSIS
- This script creates removes registry key using WMI.
- .DESCRIPTION
- This script uses WMI to get remove registry key.
- This script deletes the key and everything below
- it in the registry - use carefully!
- .NOTES
- File Name : Remove-WmiRegistryKey.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>New-WMIRegistryKey.ps1
- Key removed
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $Key = "SOFTWARE\NewKey"
- # Create key and display reslts
- $results = $reg.DeleteKey($HKEY_LOCAL_MACHINE, $Key)
- If ($results.Returnvalue -eq 0) {"Key Removed"}
Labels:
DeleteKey,
powershell,
StdRegProv,
wmi
Monday, 12 September 2011
Get-WmiRegistryBinaryValue.ps1
- <#
- .SYNOPSIS
- This script Gets and displays a registry
- binary value using WMI.
- .DESCRIPTION
- This script uses WMI to get, then display
- a binary registry Value.
- This is a re-write of a VB Sample Script.
- .NOTES
- File Name : Set-WmiRegistryBinaryValue.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>Get-WmiRegistryBinaryValue.ps1
- 54
- 46
- 4c
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $ValueName = "Example Binary Value"
- $Values = @(0x54, 0x46, 0x4C)
- $Key = "SOFTWARE\NewKey"
- # Create Value entry
- $results = $reg.GetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName)
- Foreach ($byte in $results.uvalue) {"{0}" -f $byte.tostring("x")}
W
Sunday, 11 September 2011
Set-WmiRegistryBinaryValue
- <#
- .SYNOPSIS
- This script sets a registry binary value
- using WMI.
- .DESCRIPTION
- This script uses WMI to set a binary
- registry Value.
- This is a re-write of a VB Sample Script.
- .NOTES
- File Name : Set-WmiRegistryBinaryValue.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>New-WmiRegistryBinaryValue.ps1
- Value created
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $ValueName = "Example Binary Value"
- $Values = @(0x54, 0x46, 0x4C)
- $Key = "SOFTWARE\NewKey"
- # Create Value entry
- $results = $reg.SetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values)
- If ($results.Returnvalue -eq 0) {"Value Set"}
Saturday, 10 September 2011
New-WmiRegistryValue.ps1
- <#
- .SYNOPSIS
- This script creates a new registry Value using WMI.
- .DESCRIPTION
- This script uses WMI to get create a new registry Value.
- This is a re-write of a VB Sample Script
- .NOTES
- File Name : New-RegistryKey.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>New-WmiRegistryValue.ps1
- Value created
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $ValueName = "Example_Expanded_String_Value"
- $Value = "%PATHEXT%"
- $Key = "SOFTWARE\NewKey"
- # Create Value entry
- $results = $reg.SetExpandedStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Value)
- If ($results.Returnvalue -eq 0) {"Value created"}
Labels:
SetExpandedStringValue,
StdRegProv,
wmi
Set-WmiMultiStringValue.ps1
- <#
- .SYNOPSIS
- This script sets a registry binary value
- using WMI.
- .DESCRIPTION
- This script uses WMI to set a multistring
- registry Value.
- This is a re-write of a VB Sample Script.
- .NOTES
- File Name : Set-WmiRegistryMultiStringValue.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>Set-MultiStringValue.ps1
- Value created
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $Key = "SOFTWARE\NewKey"
- $ValueName = "Example MultiString Value"
- $Values = @("Thomas", "Susan", "Rebecca")
- $Key = "SOFTWARE\NewKey"
- # Create Value entry
- $results = $reg.SetMultiStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values)
- If ($results.Returnvalue -eq 0) {"Value Set"}
Friday, 9 September 2011
New-WmiRegistryKey.ps1
- <#
- .SYNOPSIS
- This script creates a new registry key using WMI.
- .DESCRIPTION
- This script uses WMI to get create a new registry key.
- This is a re-write of a VB Sample Script
- .NOTES
- File Name : New-RegistryKey.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>New-WMIRegistryKey.ps1
- Key created
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $Key = "SOFTWARE\NewKey"
- # Create key and display reslts
- $results = $reg.CreateKey($HKEY_LOCAL_MACHINE, $Key)
- If ($results.Returnvalue -eq 0) {"Key created"}
Thursday, 8 September 2011
Get-WmiRegDword.ps1
- <#
- .SYNOPSIS
- This script gets a registry value using WMI.
- .DESCRIPTION
- This script uses WMI to get then display a resistry value, using
- the SteRegProv class and the GetDWORDValue static method.
- This is a re-write of a VB Sample Script
- .NOTES
- File Name : Get-WMIRegDword.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>Get-WmiRegDword.ps1
- Current History Buffer Size: 50
- #>
- # Define Constants
- $HKEY_CURRENT_USER =2147483649
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- #defind key/value to get
- $Key = "Console"
- $Value = "HistoryBufferSize"
- # Get Value of buffer and display
- $results = $reg.GetDWORDValue($HKEY_CURRENT_USER, $Key, $value)
- "Current History Buffer Size: {0}" -f $results.uValue
Subscribe to:
Posts (Atom)