Utilizing a script to incorporate the userPrincipalName

I'm faced with a situation where I have a CSV file containing all the details of my test script AD users. This includes information like ID number, First Name, Last Name, SamAccountName, and OU Path.

After creating these users, it turns out that some are missing the userPrincipalName attribute while the rest of the attributes are present.

To rectify this issue, I aim to write a script that will add the userPrincipalName to all users.

Here's what I've attempted so far:

$CSVusers = import-csv C:\CorpCSV.csv
$Users = $CSVusers.SamAccountName
$Users | % {Get-ADUser $_ | Set-ADUser $_ -userPrincipalName $Users}

Although the script ran without errors, it seems that nothing changed as the userPrincipalName attribute is still empty in Active Directory (View attached image).

https://i.sstatic.net/oXwic.jpg

Answer №1

To determine the desired UserPrincipleName, you must provide specific details.
It appears that this information is not included in your CSV input file.
Typically, the UserPrincipleName follows the format sAMAccountName@DnsDomainName

If all users are from the same domain you are currently logged into, you can generate the domain name using $env:USERDNSDOMAIN or

[System.Net.Dns]::GetHostEntry($env:COMPUTERNAME).HostName

$CSVusers = Import-Csv C:\PollogenCSV.csv
# create an array containing only the sAMAccountNames
$Users = $CSVusers.SamAccountName
$Domain = $env:USERDNSDOMAIN  # alternatively use [System.Net.Dns]::GetHostEntry($env:COMPUTERNAME).HostName
$Users | ForEach-Object { 
    # construct the UserPrincipalName
    $upn = "{0}@{1}" -f $_, $Domain
    Get-ADUser $_ | Set-ADUser -UserPrincipalName $upn
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on filtering an array in a JSON response based on certain conditions in Angular 7

Looking to extract a specific array from a JSON response based on mismatched dataIDs and parentDataIDs using TypeScript in Angular 7. { "data":[ { "dataId":"Atlanta", "parentDataId":"America" }, { "dataId":"Newyork", ...

Unable to run unit tests on project using my custom React library

If any of you have encountered this issue or know how to solve it, please help me. I created an NPM package that can be found at https://www.npmjs.com/package/@applaudo/react-clapp-ui It installs and runs smoothly in other projects using create react app; ...

Tips for efficiently passing TypeScript constants to Vue templates without triggering excessive reactivity

I'm curious about the most efficient way to pass a constant value to a template. Currently, I am using the data property in Vue, but I believe that is better suited for state that changes over time as Vue adds event listeners to data properties. The c ...

Developing React components with Typescript requires careful consideration when defining component props, especially when the component may be

Consider the following scenario: { this.props.userName && <UserProfile userName={this.props.userName} /> In the UserProfile component: interface UserProfileProps { userName: string; } class UserProfile extends React.Component<UserProfile ...

verifying the date in a specific moment

Is there a way to validate the date accurately? to determine whether she cleared it or not. I've exhausted all my options. Despite reading through the documentation, I am unable to get it right. Here is what I attempted: if ('2023-03-03 ...

Struggling to access my lambda function, an unfamiliar error from AWS/Serverless has presented itself

I'm currently in the process of developing a nodejs Lambda API using serverless. However, once it's deployed and I attempt to access my API endpoints, the server is throwing back an internal error. Unfortunately, CloudWatch isn't providing m ...

Can dynamic getters and setters be implemented in TypeScript?

I've recently ventured into typescript and am currently working on converting our application from es2016 to TypeScript. My current task involves creating a class with data properties and ensuring each element from the data object is accessible as a c ...

Encountering issues with accessing a variable before its initialization in the getServerSideProps function in

Currently, I am working on setting up Firebase and configuring the APIs and functions to retrieve necessary data in my firebase.tsx file. Afterwards, I import them into my pages/index.tsx file but I am encountering an issue where I cannot access exports af ...

Utilizing the index of a generic type within TypeScript

I am attempting to design generic message types that become increasingly specific, with the most precise type being inferred during the function call or class creation. Consider the following code snippet: type MessageHeaderType = 'REQUEST'|&apo ...

Typescript struggles to comprehend the nullish-coalescing operator

Within my Vue + TypeScript application, I've incorporated an external package called @moj/pagination-layout. This package utilizes the nullish operator internally. However, when attempting to run the build process, it encounters a failure and presents ...

How can I prevent Intellisense from automatically importing all global variables from Mocha (or any other testing library) into files that are not designated for testing purposes?

I am managing these files in the same directory. package.json: { "name": "example", "version": "1.0.0", "devDependencies": { "@types/mocha": "^7.0.1", "@types/node": "^13.7.1" } } tsconfig.json: {} index.ts: export const test = () =&g ...

Reactjs-ffsevents does not exist as a function

An error occurred: TypeError: fsevents is not a function. This issue is located in the FSEventsWatcher file at line 162. A new FSEventsWatcher was attempted to be created in jest-haste-map, triggering this error. The watcher creation process involved map ...

The custom marker created with Leaflet using divIcon does not seem to be styled with the specified

I'm currently working on customizing the leaflet marker using a divIcon and custom HTML. My aim is to have my marker displayed similarly to this example: https://i.sstatic.net/a5RnY.png So far, I've managed to create a marker and a divIcon with ...

Property ngIf in Angular is not being supplied by any relevant directive within the embedded template

When attempting to use ngIf, I encountered an error with a red underline. The error message states: "Property ngIf is not provided by any applicable directive on an embedded template." I then attempted to import commonModel, but received a new error: "src ...

Tips for effectively passing an array to props in Vue when leveraging Typescript and the class component decorator

I'm currently struggling to understand the proper method of passing an array as a prop to a component in Vue, using Typescript and the class component library. Following the official template, I attempted the following approach: <script lang="ts"& ...

`Is there a way to assign multiple parameters to HttpParams within Angular 5?`

I'm attempting to send multiple parameters to HttpParams in Angular 5 using the approach below: paramsObject: any params = new HttpParams(); for (let key in paramsObject) { params.set(key, paramsObj ...

Mastering the art of typing a member of an abstract generic class in Typescript with consideration for potential additional implementations

It's quite challenging to put into words, but essentially I aim to create a base abstract class outlining an abstract interface that must vary based on whether a derived class implements a specific interface or not. Here is a TypeScript playground de ...

Can anyone provide guidance on incorporating lodash into an Ionic 2 project?

Recently, I began diving into a new project that involves Ionic 2. TypeScript is still fairly new to me, and I've been brainstorming ways to integrate lodash into my project. Have any of you tackled this before and can offer guidance on how to achiev ...

Enhancing Visuals with src="imageUrl within Html"

Is there a way to customize the appearance of images that are fetched from an API imageUrl? I would like to create some space between the columns but when the images are displayed on a medium to small screen, they appear too close together with no spacing. ...

Typescript versus ES5: A comparison of Node.js server-side applications written in different languages

Note: When I mention regular JavaScript, I am referring to the ES5 version of JS. As I lay down the groundwork for a new project, my chosen tech stack consists of Node.js for the back-end with Angular2 for the front-end/client-side, and Gulp as the build ...