# using variables

**URL:** <https://forums.powershell.org/t/using-variables/351>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/using-variables/351 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/using-variables/351/1 "2011-12-31T18:30:00Z")

</div>

by MichaelPS at 2012-11-21 02:06:40

> Hi!  
>   
> I’m writing a small script, wich automaticly detects all authorized DHCP servers, and copy the DHCP database backup from those DHCP servers (which is created hourly by default) to another location.  
>   
> So far I got this:  
>   
> `Get-ADObject -LDAPFilter â€œ(&(objectClass=dHCPClass)(dhcpIdentification=DHCP Server object))â€ -SearchScope Subtree -SearchBase "CN=Configuration,DC=hv,DC=group" | foreach {Copy-Item -Path \$\C$\Windows\System32\dhcp\backup -Destination C:\DHCP_Backup$ -Recurse}`  
>   
> The first part, Get-ADObject, is working poperly. I get all authorized DHCP servers.  
>   
> But for the foreach part, I have the problem that "$_" is the Distinguished Name, not the FQDN. I also tried things like $_.Name oder $_.Properties.Name, which is not working. I guess it’s a pretty basic question, but I dont get the right way. Maybe you guys can help me out?  
> I need \server.domain\C$\Windows… for the Copy-Item line._  
>   
> _Thanks in advance!  
> Michael_

_by Klaas at 2012-11-21 02:41:04_

> _Does this work?_  
>   
> _`Get-ADObject -LDAPFilter â€œ(&(objectClass=dHCPClass)(dhcpIdentification=DHCP Server object))â€ -SearchScope Subtree -SearchBase "CN=Configuration,DC=hv,DC=group" |Select-Object -Expandproperty name |foreach {Copy-Item -Path \$`_`\C$\Windows\System32\dhcp\backup -Destination C:\DHCP_Backup$_ -Recurse}`

by MichaelPS at 2012-11-21 05:54:35

> It does! Great, thank you!

by DonJ at 2012-11-21 07:23:12

> FWIW, in your original, $\_ was actually _the entire object_ you retrieved. Not just the DN. $_.Name would be proper, but that isn’t the FQDN either._   
>   
> _Rather than piping the whole thing to ForEach-Object, I’d pipe it to Get-Member to see what properties were available._   
>   
> _What Klaas did is use Select-Object to extract the Name property’s contents for you. Where you were going wrong with $_.Name is sticking it in the middle of a string - PowerShell probably wasn’t evaluating the thing properly. ($_.Name) probably would work, too, as the parentheses force it to evaluate that expression first._  
>   
> _-Path "\$($_.Name)\C$\Windows…"  
>   
> Would be my best bet. Just so that you know what was happening.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:03pm UTC](https://forums.powershell.org/t/using-variables/351/2 "2024-05-16T21:03:33Z")

</div>


