Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function Get-RandomUser {
<#
.SYNOPSIS
Generate random user data.
.DESCRIPTION
This function uses the free API for generating random user data from https://randomuser.me/
.EXAMPLE
Get-RandomUser 10
.EXAMPLE
Get-RandomUser -Amount 25 -Nationality us,gb -Format csv -ExludeFields picture
.LINK
https://randomuser.me/
.NOTES
Author: Øyvind Kallstad
Date: 08.04.2016
Version: 1.0
#>
[CmdletBinding()]
param (
[Parameter(Position = 0)]
[ValidateRange(1,5000)]
[int] $Amount,
[Parameter()]
[ValidateSet('Male','Female')]
[string] $Gender,
# Supported nationalities: AU, BR, CA, CH, DE, DK, ES, FI, FR, GB, IE, IR, NL, NZ, TR, US
# Pictures won't be affected by this, but data such as location, cell/home phone, id, etc. will be more appropriate.
[Parameter()]
[string[]] $Nationality,
# Seeds allow you to always generate the same set of users.
[Parameter()]
[int] $Seed,
[Parameter()]
[ValidateSet('json','csv','yaml','xml')]
[string] $Format = 'json',
# Fields to include in the results.
# Supported values: gender, name, location, email, login, registered, dob, phone, cell, id, picture, nat
[Parameter()]
[string[]] $IncludeFields,
# Fields to exclude from the the results.
# Supported values: gender, name, location, email, login, registered, dob, phone, cell, id, picture, nat
[Parameter()]
[string[]] $ExcludeFields
)
$rootUrl = "http://api.randomuser.me/?format=$($Format)"
if ($Amount) {
$rootUrl += "&results=$($Amount)"
}
if ($Gender) {
$rootUrl += "&gender=$($Gender)"
}
if ($Seed) {
$rootUrl += "&seed=$($Seed)"
}
if ($Nationality) {
$rootUrl += "&nat=$($Nationality -join ',')"
}
if ($IncludeFields) {
$rootUrl += "&inc=$($IncludeFields -join ',')"
}
if ($ExcludeFields) {
$rootUrl += "&exc=$($ExcludeFields -join ',')"
}
Invoke-RestMethod -Uri $rootUrl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment