Working with IP Octets:
function Get-HostIP {
[CmdletBinding()]
[OutputType([string])]
param ()
# https://stackoverflow.com/a/44685122/4190564
$Return = (
Get-NetIPConfiguration | Where-Object {
$null -ne $_.IPv4DefaultGateway -and $_.NetAdapter.Status -ne "Disconnected"
}
).IPv4Address.IPAddress
return $Return
}
Function Get-IPOctets {
[CmdletBinding()]
[OutputType([int[]])]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$IPAddress
)
if( $IPAddress -match '(?<Octet1>\d+)\.(?<Octet2>\d+)\.(?<Octet3>\d+)\.(?<Octet4>\d+)') {
return $Matches.Octet1, $Matches.Octet2, $Matches.Octet3, $Matches.Octet4
} else {
return 0, 0, 0, 0
}
}
$Octet1, $Octet2, $Octet3, $Octet4 = Get-IPOctets (Get-HostIP)
if($Octet1 -eq 10) {
switch ($Octet2) {
{$_ -in 6..7} {$BuildingLOC = 'VM'; break}
{$_ -in 10..19} {$BuildingLOC = 'ADM'; break}
default {$BuildingLOC = 'Other'; break}
}
} else {
$BuildingLOC = 'Invalid'
}
Write-Host "IPType: $BuildingLOC"
Working with first 3 letters of Computer Name:
if($HostName = [System.Net.Dns]::GetHostName()) {
if($HostName.Substring(0, 3) -eq 'ABC') {
Write-Host "Computer name matches"
} else {
Write-Host "Computer name does not match "
}
} else {
Write-Host "No computer name"
}
EDIT:
Initially missed the question on setx /m in the comment, so here is few options related to Environment variables.
SetEnvironmentVariable/GetEnvironmentVariable:
Take a look at the examples in this answer. Note that 'Path' is the variable name in the examples, but use any name you desire. For the SetEnvironmentVariable $Value is the value being saved. And also note that there are 3 places, Process/Machine/User, in the GetEnvironmentVariable examples, which is where the variable is stored or retrieved from. I like these example because the full reference to target locations is given, but if you look through examples in this question, you can see that it can be shorted to 'Process', 'Machine', and 'User'.
- Process: I believe this is just temporary storage and the same as using $Env:MyVar. GetEnvironmentVariable can read whatever is in current session's memory, but anything set by SetEnvironmentVariable will be lost as soon as the current PowerShell session is closed.
- Machine: These variables will be available to all users, and will remain available even after a reboot. But probably not available through $Env:MyVar in the current script's session.
- User: Available to only the current user, and will remain available even after a reboot. But probably not available through $Env:MyVar in the current script's session.
setx.exe:
Any executable file can be ran from PowerShell, but there are a few things to be aware of. If you look through this answer I gave a few days ago you can see:
- You can download EchoArgs from ss64.com, execute EchoArgs instead of your exe file, setx in this case, record the pattern for the parameters passed to EchoArgs, then in PowerShell build an EchoArgs line that outputs the exact same parameter pattern, and finally replace in PowerShell EchoArgs with your exe file, setx in this case.
- Farther down in the question I introduce the use of the --% parameter/operator that causes PowerShell to stop processing the line and pass it as is.
- And then I explain how you can use environment variables to dynamically replace parts of the line after the --% parameter/operator.
A far shorter, but similar answer, can be found here. BUT, if you look Lance's answer in the same question, you can see that he uses Start-Process -FilePath $RunPath -ArgumentList $MyArgsText
to start the exe file and pass arguments to it. I still haven't taken time to experiment with Start-Process, but it looks like an interesting alternative.
REG.EXE:
Another exe file you could use is REG.EXE. If you take a look at this page you can see Machine, Process, and User described, which includes the registry keys where environment variables are stored. I actually stopped using setx in batch files a long time ago because I ran into values that setx would fail to store, but REG.EXE has no problem with.
Here are some examples I found in some batch files I have:
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V "EnableUAC" /T "REG_SZ" /D "Yes" /F>NUL 2>&1
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V "%%A" /T REG_EXPAND_SZ /D "%~2" /F
REG DELETE "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V "TemporaryMachineName" /F>NUL 2>&1
And one from a PowerShell script:
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V "PSModuleTest" 2>$null
PowerShell Registry:
I'm not going to get into editing the registry through PowerShell, but just be aware that Get-ItemPropertyValue/Set-ItemPropertyValue, Get-ItemProperty/Set-ItemProperty, Get-ChildItem/Set-ChildItem, and Get-Item/Set-Item are common built-in PowerShell commands for doing this.
For more control, and more effort on your part, you can use the following family of commands: [RegistryKey]::OpenRemoteBaseKey, [RegistryKey]::OpenBaseKey, $BaseKey.OpenSubKey, $SubKey.SetValue, $SubKey.GetValue, $SubKey.DeleteValue, and many others.