How to Display available locations in a table powershell - powershell

Hi I use the PowerShell command Get-AzureRmLocation to list the available locations from azure. I want to display them neatly in a table or some well formatted manner instead of listing them line by line.
I tried
$locations = #(Get-AzureRmLocation | Select-Object -ExpandProperty DisplayName)
Write-Host -ForegroundColor Yellow "Available Locations :"
$locations | Format-Table
But that also listing everything which makes the screen look long and not so good. is there a way?
I am getting the output as a list like this https://imgur.com/a/MjstD
I want it to be a two table column with all available locations something like that.
Thanks.

Is this what you want?
$locations = Get-AzureRmLocation
$locations | Format-Table #{n="Available Locations";e={$_.DisplayName}}

Maybe you could try to following example:
$locations = Get-AzureRmLocation
$locations | Format-Table -Property Location,DisplayName
I test in my lab, I get like below:

To list all Azure locations I would suggest using the Get-AzLocation
Get-AzLocation | Format-Table -Property Location, DisplayName
Result:
PowerShell version:

Related

Showing properties of share settings with Powershell

I need to check if this checkbox is enabled or disabled:
Is there anyway i can check this with powershell, because there something like 1400 shares I need to check?
Any help is welcome!
The cmdlet Get-SmbShare returns objects with losts of useful properties. One of which is called CachingMode.
To report on shares where the CachingMode is set to 'Manual' for instance, you can do
# as example only output on screen
Get-SmbShare | Where-Object {$_.CachingMode -eq 'Manual'} | Format-Table -Property Name, CachingMode
If you want to save this data to csv , remove the Format-Table cmd and do
Get-SmbShare | Where-Object {$_.CachingMode -eq 'Manual'} |
Select-Object Name,CachingMode |
Export-Csv -Path 'X"\Somewhere\ShareCaching.csv' -NoTypeInformation

Save the output from a Powershell command to a variable

im trying to save the output from a command to a variable,
but i just cant get it to work.
It should look something like this :
$test = (Get-SmbShare | Select Name,CurrentUsers,CurrentUserLimit | fl)
Write-Host $test
Output: The Output of Get-SmbShare
I was looking through the comments and I saw you said that you want to store it in a text file.
#Lee_Dailey Thats what i want to do, the output is gonna go into a text file. – Jan
So if you want to skip the variable part and just send it straight to a text file, you could use:
Get-SmbShare | Select Name, CurrentUsers, CurrentUserLimit | fl | Out-File -filepath "C:\Temp\file.txt"

Get-WindowsFeatures: how to filter on Display Name?

I am working with Windows Server 2016.
The PowerShell's Get-WindowsFeature cmdlet gives its output in 3 fields: Display Name, Name and Install State. As per documentation I can filter on Name field but I want to filter on Display Name field. How can I do that?
Also I can see there is hierarchy some times in values of Display Name field. For example .NET Framework 3.5 Features has 3 child items. I want to be able to filter on .NET Framework 3.5 Features Display Name field and can also see its child items. How can I do that?
You can use Where-Object and -like or -match
Get-WindowsFeature | Where-Object displayname -like '*framework 3.5*'
Get-WindowsFeature | Where-Object displayname -match 'framework 3\.5'
Even though it shows Display Name in the formatted output, it's actually DisplayName as the property name. You can see all its properties by using Get-Member
Get-WindowsFeature | Get-Member
try this:
Get-WindowsFeature | where {$_."Display Name" -like "*yoursearch*"}
$search = Get-WindowsFeature "NET-Framework-Features"
$search.subfeatures

Powershell output destroys formatting and produces gobbledegook

I've got all the information I need in a nice powershell table after running
get-mailbox | get-mailboxstatistics | ft displayname, totalitemsize
However the information gets changed to a single line of numbers and letters when I go to export it using:
| Export-Csv "C:\MailboxList.csv"
Is there a way to export the table as it's shows in the powershell shell?
Per the comment from Shaneis, this issue occurs because the ft (Formt-Table) cmdlet changes the object type and as such you cannot then use Export-Csv after it.
You should instead use Select-Object to filter your results down to the properties you want to output and then use Export-CSV after this:
get-mailbox | get-mailboxstatistics | select displayname, totalitemsize | Export-Csv "C:\MailboxList.csv"
Generally you use the Format-Table cmdlet as the last cmdlet when you want to output a nicely formatted table to the screen (although you can also send this output to a file with Out-File and it will appear in the file exactly as it does on screen).
To understand this issue in more detail, I recommend reading this article about how you should filter left, format right.

Learning PowerShell. Create usernames no longer than 8 characters and check for collision

I'm learning powershell right now.
I need to import a CSV like this:
lastname,firstname
lastname,firstname
lastname,firstname
etc
Then create a list of usernames no longer then 8 characters and check for collisions.
I have found bits and pieces of scripting around but not sure how to tie it all together.
I use Import-Csv to import my file.csv:
$variablename = import-csv C:\path\to\file.csv
but then I am not sure if I just import it into an array or not. I am not familiar with how for loops work in powershell exactly.
Any direction? Thanks.
There are a couple of concepts that are central to understanding PowerShell. Firstly, remember that you are always working with objects. So after importing your CSV file, your $variablename will refer to a collection of sub-objects.
Secondly, you can use the PowerShell pipeline to send the output of one cmdlet to the input of another. Some cmdlets will understand if you send them a collection, and automatically process each row.
If think what you're looking for though is the foreach-object cmdlet, which will allow you to run code against each item in the collection. Code inside the foreach-object block can refer to the $_ automatic variable which will contain the current object.
Assuming your CSV file is well formatted and has a header row with the column names, you can refer to each column by name e.g. $_.lastname & $_.firstname.
To put it all together:
import-csv C:\path\to\file.csv |
foreach-object {
write-host "Processing: $($_.lastname), $($_.firstname)"
# logic here to calculate username and create AD account
}
PowerShell can have a bit of a learning curve if you are coming from a different scripting environment. Here are a couple of resources that I've found helpful:
PowerShell 'gotchas' http://www.rlmueller.net/PSGotchas.htm
Keith Hill's Effective PowerShell: https://rkeithhill.wordpress.com/2009/03/08/effective-windows-powershell-the-free-ebook/
Also, check out the Technet Script Center, where there are many hundreds of Active Directory scripts. https://technet.microsoft.com/en-us/scriptcenter/bb410849.aspx
The script below should help you grasp a few concepts on how to work with csvs and manipulate data using PowerShell.
# the code below uses a 'here string' to mimic the import of a csv.
$users = #'
smith,b
smith,bob
smith,bobby
smith,sonny
smithson,john
smithson,jane
smithers,rob
'# -split "`r*`n"
$users |
ConvertFrom-Csv -Header 'surname','firstname' |
Select-Object #{Name='username'; Expression={"$($_.surname)$($_.firstname) "}}, surname, firstname |
Group-Object { $_.username.Substring(0,8).Trim() } |
Select-Object #{Name='username'; Expression={$_.Name}}, Count |
Format-Table -AutoSize
The $users | line takes the list of $users and pipes into the next command.
The ConvertFrom-Csv -Header... line converts the string into a csv.
The Select-Object #{Name... line creates an expression alias, which concatenates surname+forename. You'll notice the extra 8 spaces we append to the end of the string so we know we will have at least 8 characters in the string.
The Group-Object {... line groups the username, using the first 8 characters, if available. The .Trim() gets rid of any trailing spaces.
The Select-Object #{Name='username'... line takes the Name field from the group-object and renames to username and also shows the count from the grouping operation.
The Format-Table -AutoSize line is purely for output formatting to the console and gives you an output like the one below.
username Count
-------- -----
smithb 1
smithbob 2
smithson 3
smithers 1
An amended version of the above code, which you can use on your real csv. Change the surname, firstname column names to suit your csv.
# you would use the code below, to import your list of names
# uncomment the `# -Header surname,firstname` bit if your csv has no headers
$users = Import-Csv -Path 'c:\path\to\names.csv' # -Header surname,firstname
$users |
Select-Object #{Name='username'; Expression={"$($_.surname)$($_.firstname) "}}, surname, firstname |
Group-Object { $_.username.Substring(0,8).Trim() } |
Select-Object #{Name='username'; Expression={$_.Name}}, Count