Get-ExecutionPolicy - This will display the policy for the current session.
Set-ExecutionPolicy Unrestricted - Will allow all scripts to run.
Control this setting with GPO
Computer Configuration\Administrative Templates\Windows Components\Windows PowerShell
Create a folder
New-Item c:\bob -type Directory
Check for existance of file or folder
Test-Path c:\scripts\*.wma
Prompt user for Input
$op_file = Read-Host "Please Enter some Input?"
Delete File or Folder
Remove-Item c:\scripts\test.txt
Check if a File, Folder or Registry Key Exists
Test-Path c:\scripts\test.txt
Reboot Computer
Restart-Computer
Launch a EXE or other process
start-process "C:\windows\notepad.exe" -wait
(note the -wait switch means the script waits for the process to close before it continues the script)
Sleep or Wait (time in seconds)
Start-Sleep 10
Split a String (x,y) - Where x = number of characters from left to start, y= number of character to take
$Server = $Server.Substring(0,3) - This example takes the first 3 characters of the server name.
Combine Strings
$String3 = $string1+$String2
Managing Windows Features and Roles with Powershell
Import-module Servermanager - This loads the cmdlet
get-windowsfeature - This will list the all available features and also indicate if they are on or off
Add-WindowsFeature xxx - This will add a feature via the name (use command above to get the name)
Read file contents into a Array
$a = (get-content foo.txt)
or
$data = Import-csv "input.csv" (if this in CSV file format)
Use the contents of Array
$array = (get-content input.txt)
foreach ($element in $array)
{ do something with $element}
Copy File
Copy-item $source $Target
Set Registry Permission
$acl = Get-Acl 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
$rule = New-Object System.Security.AccessControl.RegistryAccessRule ("BUILTIN\users","FullControl","ContainerInherit","None","Allow")
$acl.SetAccessRule($rule)
$acl | Set-Acl -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
Create a Local Group
$LocalComputer = [ADSI] "WinNT://$env:computername"
$HvAdminGroup = $LocalComputer.create("Group", "Remote Desktop Power Users")
$HvAdminGroup.setinfo()
Message box for users
$WSHShell = new-object -comobject Wscript.shell
$input = $WSHShell.popup("Welcome",5,"Title")
Write-Host $data
For full syntax view
http://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx
Show the OU of the device
$filter = "(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))"
([adsisearcher]$filter).FindOne().Properties.distinguishedname
List the users Assigned to a Catalog in Xendesktop
get-brokerdesktop - filter {Catalogname -like "Catalog"} | ft -a AssociatedUserNames
Get the Date
$date = get-date
Xendesktop list desktops and users in a deployment group
$desktops = get-brokerdesktop -desktopgroupname "GB&M deliverIT"
$DeliverITDesktops = $desktops.count
forEach ($desktop in $Desktops)
{
$Users = $desktop.AssociatedUserNames
$DeliverITUsers = $DeliverITUsers + $users.count
Control a Network Service
Get-Service -Name BrokerAgent -ComputerName $computername | Stop-service
$variable = $variable.substring(5)
Grab a directory listing
# PowerShell script to list the DLL files under the system32 folder
$Dir = get-childitem 'Z:\appsense\_Archive\Daves Files.\office large users' -recurse
# $Dir |get-member
$List = $Dir | where {$_.extension -eq ".docx"}
$List | format-table directory
Get User Name
$Account = [environment]::username
Set an ACL
$path = "C:\users\$account\Documents"
$Acl = Get-Acl $path
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule($account,"Write","Deny")
$Acl.SetAccessRule($Ar)
Set-Acl $path $Acl
Fill in Array
$array = "this" , "that","and some more"
Read thru an array
ForEach ($thing in $Array)
{ do something here }
Log into a File
$StrOutput = "c:\out.txt"
Add-Content $StrOutput "This is some output $str1 $Str2 and some text"
Capture Error Output
This is somewhat hard work. An easy option is to use the try and Catch technique
Try { $example command -ErrorAction Stop }
Catch { Write-Host $_.Exception.Message }
EventLog Search
The following looks for events matching an Event ID.
Get-WinEvent -MaxEvents 1 -FilterHashTable @{ LogName = "System"; ID = 7001 }
Functions
Function Myfunction1 ($a,$b,$c)
{
Stuff
}
Then call it with MyFunction1 a,b,c
No comments:
Post a Comment