Export Group Members to a Csv
This PS command list and export Group Members of a Group
Get-ADGroupMember -Identity “GroupNamex” -recursive | Get-ADUser -Properties samaccountname | select samaccountname | Export-Csv -Path “c:\myexport.csv”
How To Get Machine ID of a local Computer
Get-WmiObject -class SoftwareLicensingService | Select-object ClientMachineID
Add Computers to an AD Group from CSV file
We need to prepare the data.csv file with computer names only (do not add any titles at the top)
Then create a ps1 file and add the following script and run. Just make sure you change SCCMClient portion with your own group name.
$laptops = Get-Content “data.csv”
foreach ($laptop in $laptops) {
$obj = Get-ADComputer $laptop
Add-ADGroupMember -ID SCCMClients -Members $obj
}
Get Computers with a specific names and export to CSV file
Get-ADComputer -Filter * | Where-Object {$_.Name -like “NPI*”} | Select -Property Name | Sort Name | Export-Csv -Path “c:\data.csv”
Find AD Users whose specific attribute is null (empty)
The following PS code brings the users whose userprincipalname field is empty
Get-ADUser -Filter * -properties userprincipalname | where userprincipalname -eq $null | Format-Table -Property Name
Create DHCP Reservations with Powershell
One of my colleagues asked me to create multiple DHCP reservations by using a csv file. Here is how we can do that.
First we need to create the csv file in the following format
ScopeId,IPAddress,Name,ClientId,Description
10.50.32.0,10.50.32.250,test,1a1b1c1d1e1f,test desc
There might be a confusion between Scope Id and Scope name. Your Scope Id is basically in the ip address format, not a name. Scope Id in the scope below is 10.200.0.0
Just run the following command
Import-Csv dhcp.csv | Add-DhcpServerv4Reservation
PowerShell Script that deletes files which starts with a specific string and older than specific days
Here is the powershell script named deleteTempPhpFiles.ps1. I placed that file D:\DeleteTempPHP
$Path=”C:\Windows\Temp”
$Files=”php*.*”
Get-ChildItem $Path -Include $Files -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-15))} | Remove-Item
You can run the script with a schedule task, Under the Actions Tab of Task Scheduler, make sure you define the following fields as follows
Action: Start s program
Program/Script: powershell.exe
Add Arguements: -ExecutionPolicy Bypass -file “D:\DeleteTempPHP\deleteTempPhpFiles.ps1”
Move User Accounts to another OU based on department
Greetings!
Today I needed to move the existing Active Directory user accounts from their OU to another OU based on the users’ department. Below you can download the csv and script files written by Prashant Dhewaju (MS Repository). Usage is pretty simple.
Script
move.csv
Run Active Directory Module for Windows Powershell on your domain controller as the Administrator.
I copied these 2 files under c:\Scripts\BulkuserMove, then run the following command.
C:\Scripts\BulkuserMove> .\Bulkusermove.ps1