Technically yes BUT the Windows version you have on the M.2 will recognise that the serial number of the mainboard is different and start to tell you that the version is no longer on the same computer. If you have linked your Windows 10 key with an online Microsoft account, this problem can be solved by deleting the old PC from the licences in the Microsoft account. ATTENTION: It is highly
recommended to read the product key on the old PC with a Windows 10 product key reader in order to reactivate the old Windows 10 version on the new PC. You can also do this on your new PC and use the Windows 10 product key as desired. You should also remove the new M.2 in the new PC first, otherwise the PC will boot the new operating system. Alternatively, you can also use an image programme like this one Acronis True Image 2021 Box 1 (Unlimited, 1x, Android, iOS, Windows, macOS) to make an image of your old computer and copy it back to the new one. It is then virtually a one-to-one copy.
However, I would recommend that you only transfer the data you really need to the new device. It's a clean thing to do and you can usually reinstall the programmes you need without further ado.