Friday, June 3, 2016

Quickies


Boolean existence check
Test-Path -Path <Path to check>
returns True or False

Split a variable into two new variables
$RemGroupID, $RemVersionID = $CGDef.Split(" ",2) 

Get  the logged on User SID
$GetUserSID = whoami.exe /USER /NH
$UserName, $UserSID = $GetUserSID.Split(" ",2)

Get the latest file and create an object with no empty lines
#get the most recent *CGClearList.txt file
$CGCLFileLoc = $env:PUBLIC+"\*CGClearList.txt"
$CGCLFile = get-childitem $CGCLFileLoc | sort lastwritetime | select -last 1 -expand Name
$CGCLFileLoc = $env:PUBLIC+"\"+$CGCLFile


#Create an object with no empty lines
$CGCLObj = get-content $CGCLFileLoc | where {$_ -ne ""}
 

Get the a user's logon history -it will probably return events of GroupPolicy associated with the user logging on.
Get-Eventlog system -username *<username>


Search for users in a target OU with a property which matches a critera
get-aduser -SearchBase "OU=xxx,OU=xxx,OU=Administration,DC=xxx,DC=xxx,DC=local" -Properties employeeid -Filter {name -like "*"} | ?{$_.employeeid -notlike $null} | select name,employeeid

List all the properties for the class
get-wmiobject -computername "$TargetName" -namespace root\CIMV2 -Class Win32_OperatingSystem |sort name |select-object [a-z]*

Check a file exists
if ([System.IO.File]::Exists($FilePath))

Create an Array
$myarray = @(1,2,3)

List Environmental Variables
dir env:

Crazy File copy command - This copies all the files in the subdirectories to the current directory.  Run it from the folder where you want all the files copied to
Copy-item -path C:\test\music -filter *.mp3 -recurse -Container:$false

Tabs and Character Returns
$line = "1"+"`t"+"2"+"`r`n"+"3"
$line
Outputs
1    2
3