Function to run conditional on CSV and delete line if true

It's Friday and my brain is focusing more on the weekend than work, so I ask your help, oh PowerShell community:
I've been tasked with converting our old VB new user script to Powershell, the input of which is a CSV that HR sends us on a daily basis. On rare occasion they include someone who already exists as a user, and I'd like to write a function to check the CSV
for that and remove that line in the CSV before continuing processing. So far I have the following:
Function Clear-Existing
param
[string]$path
$csv_input = import-csv -Delimiter ',' $path -Header @("EmpID","LastName","FirstName","Title","Dept","Status") | % ( $_.EmpID
foreach ($entry in $csv_input)
if (Check-ADUser $entry.EmpID -eq $true)
write-host "user $entry already exists!"
But being said Friday, I can't figure out how to remove the "offending" line from the CSV and save it.
[email protected]

Try something like this, you may want to change the path on the Export-Csv file to verify first, so that way you don't overwrite your file
Function Clear-Existing
param
[string]$path
$results = @()
import-csv -Delimiter ',' $path -Header @("EmpID","LastName","FirstName","Title","Dept","Status") | ForEach {
if (Check-ADUser $_.EmpID -eq $true)
write-host "user $entry already exists!"
else
$results += $_
$results | Export-Csv $path
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Don't Retire Technet

Similar Messages

  • Implementing Conditional Insert, Update, and Delete Allows for View Objects

    Steve,
    Thanks for the info in this article it was very helpful. I have one question as I created a test case that used dept & emp tables as master and detail.
    Placing code in the emp detail ViewRowImpl that also tests to see if the master is updateable and it all works fine but I noticed that when you run it in the tester the dept master fields are grayed out but not the emp details and if you bring up the emp detail with out the view link then they are grayed out.
    Just wondering is this just a fact of the detail being shown in a table in the master detail and not a form or is there more to it then that??

    yes as follows:
    isMasterUpdateable checks the Dept table to see if it is updateable before allowing emp to be updated.
    public boolean isAttributeUpdateable(int index)
    if ( isMasterUpdateable() == true )
    return ( super.isAttributeUpdateable(index) );
    return(false);
    When I run this in the tester the results are correct except for the visual where the form is grayed out but the detail in the table is not. Should the table gray out also or is this effect something that must be coded in all Jtables that i may implement? If it needs to be coded to get the effect where does Jtable check for isAttributeUpdateable ?
    thanks,

  • Just got win8 and can't do things I could before - fb apps, shopping on line, etc., don't function right. run ghostery, noscript, adblock and donottrackme.

    many fb apps will no longer post to my timeline, when I try to shop on-line and click to ad something to my order, nothing happens. I've tried allowing most scripts on a particular page, temporarily suspending adblock, etc., with no improvement. idk if it's a problem between firefox and win8 or one of the add ons...??

    To see whether the extensions are the root of your troubles, try visiting Facebook in Firefox's Safe Mode. That's a standard diagnostic tool to bypass interference by extensions (and some custom settings). More info: [[Troubleshoot Firefox issues using Safe Mode]].
    You can restart Firefox in Safe Mode using
    Help > Restart with Add-ons Disabled ''(Plugins like Flash will still work)''
    In the dialog, click "Start in Safe Mode" (''not'' Reset)
    Any difference?

  • Read line and delete line

    Hi Experts ,
    I am doing a report on material history where I need to find out open orders....if suppose a matnr does'nt have Po then also I need to display as it will have open PR.
    Now I want to delete the line where ebeln eq space but I can't put the condition as:
    delete it_first where ebeln = space as single lines will also get deleted.
    My condition should be if a matnr is displayed in more than 1 line and if ebeln eq space
    delete it_first.
    loop at it_first.                              " I facing problem with this code
      if it_first-wemng GE it_first-menge.
        move '   '  to it_first-ebeln.
        move '   '  to it_first-menge
        move '   '  to it_first-wenge
    modify it_first.
    endif.
    endloop.
    after the above statement my O/P is as below
    werks   ,   matnr   ,     ebeln        ,  menge  , wemng
    UNIT1   ,    ABCD   ,   450025612       ,    256       ,   125
    UNIT1   ,    ABCD   ,   450012345       ,    756       ,   456
    UNIT1   ,    ABCD   ,  --SPACE--         ,   0.00       ,  0.00     "Need to delete this line
    UNIT1   ,    ABCD   ,  --SPACE--         ,   0.00       ,  0.00     "Need to delete this line
    UNIT2   ,  BEDF     ,   --SPACE--        ,   0.00       ,  0.00    " this needs to be displayed (just 4 matnr)
    Please advice
    Karthik

    Hi Karthik,
    Hi Dileep,
    Its working, but only for the first matnr ....but the matnr diplayed after the first matnr is not working..
    Thanks
    Karthik
    Just to Memorise and confirm, in your post I seen you need to delete only EBELN's when space(null or blank)
    Only if there is an order or duplicate... {as commented beside in the code}.
    The code works fine... Here is the code What i tested in my system and working fine.
    If problem still exists, please update with more records, lets say 7 -15 records,,,,
    REPORT  ZSAMPLE.
    DATA P_MATNR TYPE MATNR.
    DATA: BEGIN OF IT_FIRST OCCURS 0,
    WERKS TYPE WERKS,
    MATNR TYPE MATNR,
    EBELN TYPE EBELN,
    MENGE TYPE MENGE,
    WEMNG TYPE WEMNG,
    END OF IT_FIRST.
    IT_FIRST-WERKS = 'UNIT1'.IT_FIRST-MATNR = 'ABCD'.IT_FIRST-EBELN = '450025612'.IT_FIRST-MENGE = '256'.IT_FIRST-WEMNG = '125'.
    APPEND IT_FIRST.
    IT_FIRST-WERKS = 'UNIT1'.IT_FIRST-MATNR = 'ABCD'.IT_FIRST-EBELN = '450012345'.IT_FIRST-MENGE = '756'.IT_FIRST-WEMNG = '456'.
    APPEND IT_FIRST.
    IT_FIRST-WERKS = 'UNIT1'.IT_FIRST-MATNR = 'ABCD'.IT_FIRST-EBELN = ' '.IT_FIRST-MENGE = '0'.IT_FIRST-WEMNG = '0'.
    APPEND IT_FIRST.
    IT_FIRST-WERKS = 'UNIT1'.IT_FIRST-MATNR = 'ABCD'.IT_FIRST-EBELN = ' '.IT_FIRST-MENGE = '0'.IT_FIRST-WEMNG = '0'.
    APPEND IT_FIRST.
    IT_FIRST-WERKS = 'UNIT2'.IT_FIRST-MATNR = 'BEDF'.IT_FIRST-EBELN = ' '.IT_FIRST-MENGE = '0'.IT_FIRST-WEMNG = '0'.
    APPEND IT_FIRST.
    IT_FIRST-WERKS = 'UNIT2'.IT_FIRST-MATNR = 'BEDF'.IT_FIRST-EBELN = ' '.IT_FIRST-MENGE = '0'.IT_FIRST-WEMNG = '0'.
    APPEND IT_FIRST.
    IT_FIRST-WERKS = 'UNIT1'.IT_FIRST-MATNR = 'BEDF'.IT_FIRST-EBELN = '450012345'.IT_FIRST-MENGE = '756'.IT_FIRST-WEMNG = '456'.
    APPEND IT_FIRST.
    SORT IT_FIRST BY MATNR ASCENDING EBELN DESCENDING.
    LOOP AT IT_FIRST.
      IF P_MATNR EQ IT_FIRST-MATNR AND IT_FIRST-EBELN EQ SPACE.
        DELETE IT_FIRST.
      ENDIF.
      P_MATNR = IT_FIRST-MATNR .
    ENDLOOP.
    Thanks & regards,
    Dileep .C

  • What are the F keys for and what are the insert and delete line shortcuts?

    I have always had a full keyboard and I'm really missing a few shortcuts I previously had that I used mostly in Quickbooks...mainly using the Control insert combination for inserting a line in General Journal and also the Control delete for deleting a line. Is this just an issue with Quickbooks on a Mac? I hate to say it, but during a period of insanity and persuasion by my accountant, I...GASP, went to a PC from a Mac and now I'm finally back in the family. I've forgotten a lot of what I'd learned in the last several years. Do the F keys have these capabilities or is there some other way? Thank you for any assistance.

    Look carefully at the F keys you will see that F1 and F2 will darken and Brighten yur screen F 10 11 and 12 will Mute turn down and turn up your sound.F3 will minimise the screen F4 goes to all your Applications installed.
    You need to fool around and see what they all do to help minimize work
    Cheers
    Don
    http://support.apple.com/kb/HT1343
    http://macmost.com/mfm/mastering-keyboard-shortcuts.html

  • Function Module for blocking, unblocking and deleting assets

    Hi,
    Are there function modules available for blocking,unblocking and deleting assets in SAP.
    Can I user 'BAPI_FIXEDASSET_CHANGE' for the same?
    Thanks in adavnce,
    Laxman

    Hey,
    I tried by putting the values of company code,asset number,subnumber, transaction type as 'A' and passing the value of INLA-XSPEB as 'X' in FM ASSET_MASTERRECORD_MAINTENANCE to block the asset but it is not working. I am trying many other things. Can you suggest what are the mandatory fields that need to be passed for the asset to be blocked.
    As there are lot of parameters in this FM, I am little confused. Is there a proper documentation available somewhere to understand the flow.
    Thanks in advance for the help.
    Laxman Pai.

  • CSV and Chkdsk scan need alerts

    Hi
    We have configured Hyper V clustering with Windows 2012 R2 DC. All the disks are configured as CSV, later installed guest OS and applications and everything is working fine as of now. Now most of teh CSV are showing the information Chkdsk scan
    needed. We are looking for a solution to correct this / run Chkdsk on CSVs and if required how frequently do we need to run chkdsk on CSVs
    Thanks in advance   
    LMS

    We have configured Hyper V clustering with Windows 2012 R2 DC. All the disks are configured as CSV, later installed guest OS and applications and everything is working fine as of now. Now most of
    teh CSV are showing the information Chkdsk scan needed. We are looking for a solution to correct this / run Chkdsk on CSVs and if required how frequently do we need to run chkdsk on CSVs
    You don't need to do it on a routine basis! Only if you're asked to do so.
    When you do follow these links as performing chkdsk would obviously affect performance. See:
    http://technet.microsoft.com/en-us/library/cc772587.aspx
    http://technet.microsoft.com/en-us/library/ee461010.aspx
    http://itinfras.blogspot.com/2010/04/all-about-maintennance-mode.html
    http://blogs.msdn.com/b/clustering/archive/2014/01/01/10486462.aspx
    Good luck!
    StarWind VSAN [Virtual SAN] clusters Hyper-V without SAS, Fibre Channel, SMB 3.0 or iSCSI, uses Ethernet to mirror internally mounted SATA disks between hosts.

  • Advanced table copy and delete slow when done programatically

    I am modifying a line in an advanced table, then copying it and deleting it. What I noticed is that
    it takes a lot less time to copy and delete manually within the application verses doing it programatically.
    To copy or delete 10 lines it takes about 1 or 2 seconds within the form. When I do this programatically
    it can take up to 20 seconds. Does anyone know why it takes so much longer?
    Here is a sample of my code.
              porequisitionlinesvorowimpl.setSelected("Y"); //programatically select line for copy
         ArrayList deletearraylist = null;
         String s1 = porequisitionlinesvorowimpl.getItemNumber();
              java.sql.Connection connection = oadbtransaction.getJdbcConnection();
              //Query values from for child item.
              OraclePreparedStatement oraclepreparedstatement = null;
              ResultSet resultset = null;
              try {oraclepreparedstatement = (OraclePreparedStatement)connection.prepareStatement("SELECT I2.DESCRIPTION WHERE                       I.SEGMENT1 = ?");
                               int i = 1;
                               oraclepreparedstatement.defineColumnType(i++, 12, 240);                                                                 oraclepreparedstatement.setString(1, s1);
                               resultset = oraclepreparedstatement.executeQuery();
                               while(resultset.next())
                                    //Collect values from query for child item.
                                    String desc = resultset.getString(1);
                              porequisitionlinesvorowimpl.setItemDescription(desc);
                              //Copy modified parent to produce a child line.
                              ArrayList copyarraylist = new ArrayList(2);
                               copyarraylist.add("populateAndDuplicate");
                               copyarraylist.add("lines");
                               executeServerCommand(oapagecontext, oaapplicationmodule, "CheckoutLinesSvrCmd", copyarraylist);
                               } //while loop for resultset
              resultset.close();
         oraclepreparedstatement.close();
              catch(SQLException sqlexception) {
                   ErrorUtil.handleFatalException(oadbtransaction, sqlexception, this);
              //Delete original Kit Parent           
              deletearraylist = new ArrayList(2);
              deletearraylist.add("populateAndDeleteReqLines");
              deletearraylist.add("lines");
              executeServerCommand(oapagecontext, oaapplicationmodule, "CheckoutLinesSvrCmd", deletearraylist);

    I timed these too and they appeared to completed very quickly. The only events that took a significant amount of time where the calls to the executeServerCommand when I copied and deleted lines.
    Can the database connection and looping the resultset be slowing these activities down?
    There is no VO existing that generates the query results I am looking for. Do I need to create my own VO?

  • Using iPad mail program I have a draft of an email. I attempted to "cancel" and "delete draft" but it is still on my screen even after shutting down and restarting. How do I exit so that I can use other mail functions?

    Using iPad mail program I have a draft of an email. I have attempted to "cancel" this and "delete draft" but the email stays on my screen. How can I exit this so that I can use other email functions?

    Try closing the Mail app completely and see if it works properly when you re-open the app : from the home screen (i.e. not with the Mail app 'open' on-screen) double-click the home button to bring up the taskbar, then press and hold any of the apps on the taskbar for a couple of seconds or so until they start shaking, then press the '-' in the top left of the Mail app to close it, and touch any part of the screen above the taskbar so as to stop the shaking and close the taskbar.
    If that doesn't work also do a reset : press and hold both the sleep and home buttons for about 10 to 15 seconds (ignore the red slider), after which the Apple logo should appear - you won't lose any content, it's the iPad equivalent of a reboot.

  • My iPod touch will not let me download more apps, though I have sufficent memory space, and other internet based functions are running...

    though I have sufficent memory space, and other internet based functions are running...

    Try the following to rule out a software problem:                 
    - Reset the iOS device. Nothing will be lost
    Reset iOS device: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Power off and then back on the router
    - Reset network settings: Settings>General>Reset>Reset Network Settings
    - iOS: Troubleshooting Wi-Fi networks and connections
    - iOS: Recommended settings for Wi-Fi routers and access points
    - Restore from backup. See:
    iOS: How to back up
    - Restore to factory settings/new iOS device.
    If still problem make an appointment at the Genius Bar of an Apple store since it appears you have a hardware problem.
    Apple Retail Store - Genius Bar
    However, since it works with other networks that really suggests a problem with your network.

  • If I have a backup on iCloud from my old phone in my new phone will it delete my old information? I'm running out of GB and i'm not sure if I should delete it or not. Please help!

    I had an old iphone 4s that was backed up on icloud. When I got a new iphone 4s, the back up from my old iphone and new iphone stayed. Now i'm running out of GB and my iphone hasnt been backed up in weeks. If I delete the backup from my old iphone on my new iphone to clear space, will it delete the information I had in my old iphone, which is now in my new iphone? help!!! thanks.

    if you go to 'settings - iCloud - storage and backup' and remove an old backup, it won't do anything to the current settings/information on your phone, it will only remove the backup.

  • I am running OS10.6.8 and have a mail box duplication. I use gmail and when I open my mail I have both the Apple Mail and another set of boxes for Gmail. Both get all mail and when I delete from one, it deletes from the other. How can I get rid of the dup

    I am running OS10.6.8 and have a mail box duplication. I use gmail and when I open my mail I have both the Apple Mail and another set of boxes for Gmail. Both get all mail and when I delete from one, it deletes from the other. How can I get rid of the dup

    Hi,
    According to your descriptioin, I don't think this is system problem, it should be Intel driver problem. It would be contact Intel to confirm this issue whether this is their driver problem.
    Roger Lu
    TechNet Community Support

  • Search and Delete a specific record from a CSV file

    Hi All,
    I am new to java . I want to search for the records from CSV file and delete the row form the file.
    Below is my Sample .csv
    100||a100||1b100
    200||b200||dc300
    200||bg430||ef850
    400||f344||ce888
    Now I need some help in below requirements.
    1.How to delete a record having value 200 and b200?
    2.If record already exists how to update the existing record with new values?
    Please share your ideas or give me some code snippet..
    Thanks in Advance

    In that case Do i need to write the entire contents of my file to a hash table(sumthng like this) and modify the Second row in my case with the new values..
    is it possible??I would have done like this (though there maybe better methods)
    1- create a class representing the record.
    class Record{
          String field1;
          String field2;
          String field3;
          // and so on....
          //setters
          public void setFeild1(String str){
              field1=str;
          // and so on....
          //getters
          public String getFeild1(){
              field1=str;
          // and so on....
          public String toString(){
               return(field1+"||"+field2+"||"+field3);
    }//end class2- then create an ArrayList meant to have objects of this class (Generics).
    3- read from the file , create a new Record Object and add that to the ArrayList
    4- perform operations on the ArrayList (you can add new records, and delete record, update......)
    5- write the record back to file using 'toString()' method.
    is there ne sample code available for thisdon't know, but you rarely get full code on forums.....outline given can be followed
    Thanks!
    Edit: It appears that 'r035198x' and me have the same point. This shows that this methodology is almost a standard way( if we ignore the Random access files.....)
    Edited by: T.B.M on Jan 13, 2009 2:39 PM

  • I have a back up of the folder 'iTunes Media' on a Nas (LaCie CloudBox). I do a weekly update. Everything is cool.Now I would  love to keep the folder on the Nas and delete the one on my internal HD. What kind of trouble i may run into?

    I have a back up of the folder 'iTunes Media' on a Nas (LaCie CloudBox).
    I do a weekly update.
    Everything is cool.
    Now I would  love to keep the iTunes Mediafolder on the Nas and delete the one on my internal HD.
    What kind of trouble I may run into?
    Where are the hidden catches?
    What can go wrong, and how can i undo any mistake?
    I also have an iTunes Match acccount.
    Cheers
    rudy

    HI
    Read this user tip Make and keep a backup of your iTunes library, and only stream from the cloud.
    Problems? No hidden catches. Whilst streaming using match, you will need a wifi connection. You main worry is that if your NAS drives fails, you need to redownload your music from match but this could take time depending on how large your collection is. Personally I keep an additional backup of all my music as a matter of course.
    Jim

  • Thoroughly angry and frustrated. I've run out of room and need to make more to add more songs. Once and for all, how do I, if I even CAN, delete music from my iPod Nano WITHOUT losing them from iTunes?

    Thoroughly angry and frustrated. I've run out of room and need to make more to add more songs. Once and for all, how do I, if I even CAN, delete music from my iPod Nano WITHOUT losing them from iTunes?

    You should take the time to familiarize yourself with the documentation that is available.  You can "uncheck" a song in iTunes, and then do a manual update.

Maybe you are looking for