none
Powershell - copying part of a value and putting it into a new value RRS feed

  • Question

  • Good afternoon,

    I am having trouble finding anything about a problem I'm facing and as my knowledge in powershell isn't that advanced, I'm hoping you guys can help me out.

    I have a .csv file called File1.csv containing AD values which I import in Powershell with Import-Csv.

    I have the value UserPrincipalName which contains a full e-mail address ''testuser@testcompany.com'' and I am able to put this in a new value called mailNickname but I want it without the @testcompany.com and I want to do this so it can be used for any user that comes across so I'm not restricted to editing the script per user.

    What I currently got and is giving me the full ''testuser@testcompany.com'' e-mail output which I don't want:

    Import-Csv -Path D:\File1.csv |

    Select-Object *,@{Name='mailNickname';Expression={"$($_.UserPrincipalName)"}}

    Export-Csv -Path D:\File2.csv -NoTypeInformation

    Thanks in advance.


    Thursday, March 17, 2016 3:36 PM

Answers

  • Hi,

    You can use .Split for this:

    Import-Csv .\inputFile.csv | 
        Select *,@{N='mailNickname';E={$_.UserPrincipalName.Split('@')[0]}} |
            Export-Csv .\outputFile.csv -NoTypeInformation


    Thursday, March 17, 2016 3:43 PM
  • Hi,

    You can use .Split for this:

    Import-Csv .\inputFile.csv | 
        Select *,@{N='mailNickname';E={$_.UserPrincipalName.Split('@')[0]}} |
            Export-Csv .\outputFile.csv -NoTypeInformation


    My shift is done in a minute and I got appointments after that so I won't be able to test it right now but I will tomorrow morning. Thanks!

    EDIT: Works like a charm, thanks!
    Thursday, March 17, 2016 3:50 PM

All replies

  • Hi,

    You can use .Split for this:

    Import-Csv .\inputFile.csv | 
        Select *,@{N='mailNickname';E={$_.UserPrincipalName.Split('@')[0]}} |
            Export-Csv .\outputFile.csv -NoTypeInformation


    Thursday, March 17, 2016 3:43 PM
  • Hi,

    You can use .Split for this:

    Import-Csv .\inputFile.csv | 
        Select *,@{N='mailNickname';E={$_.UserPrincipalName.Split('@')[0]}} |
            Export-Csv .\outputFile.csv -NoTypeInformation


    My shift is done in a minute and I got appointments after that so I won't be able to test it right now but I will tomorrow morning. Thanks!

    EDIT: Works like a charm, thanks!
    Thursday, March 17, 2016 3:50 PM
  • Cheers, you're very welcome.

    Friday, March 18, 2016 12:09 PM