Solved

How to use GC command in PowerShell?

Posted on 2013-12-06
4
650 Views
Last Modified: 2013-12-06
How can I import a TXT file using GC?  Right now I have the following code, but I want to pull it from a txt file that does not have a header... it's just a list of UPNs.  Would I change the first line to "$Users = GC .\UPNs.txt"?  If so, since there is no header, $_.userPrincipalName will no longer work and I wouldn't know what to change it to.

I tried this as a test, but it's not working...
$Users= GC .\zTest.txt
$Users | foreach-object {
write-host $_
}

Thanks in advance!


# Import list of user UPNs
$Users = Import-Csv .\UPNs.csv

# Loop thru users
$Users | ForEach-Object {

Get-MsolUser -UserPrincipalName $_.userPrincipalName
Set-MsolUser -UserPrincipalName $_.userPrincipalName -UsageLocation $UsageLocation

if ($DLtype -eq "1") 
{write-host "Activating User"

Set-MsolUserLicense -UserPrincipalName $_.userPrincipalName -AddLicenses $AccountSkuId -LicenseOptions $options
}

Open in new window

0
Comment
Question by:bpl5000
  • 2
  • 2
4 Comments
 
LVL 5

Author Comment

by:bpl5000
ID: 39701891
Oh, looks like the small test I had is working.  So I guess I would just change $_.userPrincipalName to $_?
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39701896
Yes, that's correct.
Also, if you had a .CSV without headers, you could manually specify them with the -header parameter of Import-CSV.
0
 
LVL 5

Author Comment

by:bpl5000
ID: 39701935
Oh, okay... so I would use "$Users = Import-Csv -headers .\UPNs.csv"?
0
 
LVL 40

Expert Comment

by:footech
ID: 39701958
You could use
$Users = Import-Csv -header "userprincipalname" .\UPNs.csv

Open in new window

It's just an option that I thought I'd mention, could be useful if there're other properties you might want to add to the objects.
But outside of that, with just a single column I would go the GC route.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question