how to run script with AD cmdlet on computers in the domain

hi all,

First time posting and a new lover of powershel :slight_smile:

We were asked to install printers using powershell (as it is faster then group policies) over the network.
In our organisation there a “number of printers” more then 50.

We are using the Location properties in the computer account in AD to allocate the printer to the computer (allocation based on location)
so based on the hostname of the local machine we find the location in the AD computer account and we can assign the printer.

being new to powershell I have charged ahead and created the script with the help of my friend TIM who is new to powershells as well.
But it will not run because we are using on of the AD cmdlet are not installed on the PCs about 1000. IS there away around this ?

$localcomputer = hostname
[Microsoft.ActiveDirectory.Management.ADComputer] $henry = Get-ADComputer -Identity $localcomputer -Properties location

function AddPrinter {
if($location -eq “$$$$$$$”){
Write-Host " \printservername\printername"; Add-Printer -ConnectionName “\printservername\printername”


if($location -eq "$$$$$$"){
    Add-Printer -ConnectionName "\\printservername\printername"


if($str.Trim().Length -le 0){
#Default Case if no location set
Write-Host “Default:”
#Write-Host " \printerservername\printername"; Add-Printer -ConnectionName “\printerservername\printername”
foreach($location in $locations){
if($location.Length -gt 0){
$campus=$tokens[0].Trim() +"-" +$tokens[1].Trim()
AddPrinter $campus


Welcome to the fun that is PowerShell!

Although you could install AD cmdlets on all the machines. More likely though you have to use an alternative such as ADSI.

$adsiFinder = “(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))”
$ADMachine = ([adsisearcher]$adsiFinder ).FindOne()

Note, that using adsiFinder, if you have multiple domains in the same forest, you might end up with a machine with the same name, so you may want to verify the domain first if that is a possible issue for you.

Hi Raymond,

Sorry about the late reply I had problem logging to the site each time I logged in and looked for my thread the site logged me of.
I just wanted to say thank you very much as you put me in the right direction.

I ended up doing this:

Function PCLocationPropertieInAD {

$file= get-content  "filepathcontaingtheloactions"

Foreach ($line in $file) {

    $items = $line.split(",")
    $computername= $items[0]
    $location = $items[1]

$location, $computername

Set-ADComputer -Identity $computername -Location $location